-
Notifications
You must be signed in to change notification settings - Fork 1
/
schemaregistry_client.go
785 lines (698 loc) · 24.1 KB
/
schemaregistry_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
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
/**
* Copyright 2022 Confluent Inc.
* Copyright 2023 Jerome Bidault (jeromedbtdev@gmail.com).
*
* 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.
*
* This file has been modified by Jerome Bidault (jeromebdtdev@gmail.com) to include additional functionality.
*/
package schemaregistry
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"sync"
"github.com/djedjethai/gokfk-regent/cache"
)
/* Schema Registry API endpoints
*
* ====Schemas====
* Fetch string: schema(escaped) identified by the input id.
* -GET /schemas/ids/{int: id} returns: JSON blob: schema; raises: 404[03], 500[01]
*
* ====Subjects====
* Fetch JSON array str:subject of all registered subjects
* -GET /subjects returns: JSON array string: subjects; raises: 500[01]
* Fetch JSON array int:versions
* GET /subjects/{string: subject}/versions returns: JSON array of int: versions; raises: 404[01], 500[01]
*
* GET /subjects/{string: subject}/versions/{int|string('latest'): version} returns: JSON blob *schemaMetadata*; raises: 404[01, 02], 422[02], 500[01]
* GET /subjects/{string: subject}/versions/{int|string('latest'): version}/schema returns : JSON blob: schema(unescaped); raises: 404, 422, 500[01, 02, 03]
*
* Delete subject and it's associated subject configuration subjectConfig
* -DELETE /subjects/{string: subject}) returns: JSON array int: version; raises: 404[01], 500[01]
* Delete subject version
* -DELETE /subjects/{string: subject}/versions/{int|str('latest'): version} returns int: deleted version id; raises: 404[01, 02]
*
* Register new schema under subject
* -POST /subjects/{string: subject}/versions returns JSON blob ; raises: 409, 422[01], 500[01, 02, 03]
* Return SchemaMetadata for the subject version (if any) associated with the schema in the request body
* -POST /subjects/{string: subject} returns JSON *schemaMetadata*; raises: 404[01, 03]
*
* ====Compatibility====
* Test schema (http body) against configured comparability for subject version
* -POST /compatibility/subjects/{string: subject}/versions/{int:string('latest'): version} returns: JSON bool:is_compatible; raises: 404[01,02], 422[01,02], 500[01]
*
* ====SerializerConfig====
* Returns global configuration
* -GET /config returns: JSON string:comparability; raises: 500[01]
* Update global SR config
* -PUT /config returns: JSON string:compatibility; raises: 422[03], 500[01, 03]
* Update subject level subjectConfig
* -PUT /config/{string: subject} returns: JSON string:compatibility; raises: 422[03], 500[01,03]
* Returns compatibility level of subject
* GET /config/(string: subject) returns: JSON string:compatibility; raises: 404, 500[01]
*/
// Reference represents a schema reference
type Reference struct {
Name string `json:"name"`
Subject string `json:"subject"`
Version int `json:"version"`
}
// SchemaInfo represents basic schema information
type SchemaInfo struct {
Schema string `json:"schema,omitempty"`
SchemaType string `json:"schemaType,omitempty"`
References []Reference `json:"references,omitempty"`
Subject []string `json:"subject,omitempty"`
}
// MarshalJSON implements the json.Marshaler interface
func (sd *SchemaInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Schema string `json:"schema,omitempty"`
SchemaType string `json:"schemaType,omitempty"`
References []Reference `json:"references,omitempty"`
Subject []string `json:"subject,omitempty"`
}{
sd.Schema,
sd.SchemaType,
sd.References,
sd.Subject,
})
}
// UnmarshalJSON implements the json.Unmarshaller interface
func (sd *SchemaInfo) UnmarshalJSON(b []byte) error {
var err error
var tmp struct {
Schema string `json:"schema,omitempty"`
SchemaType string `json:"schemaType,omitempty"`
References []Reference `json:"references,omitempty"`
Subject []string `json:"subject,omitempty"`
}
err = json.Unmarshal(b, &tmp)
sd.Schema = tmp.Schema
sd.SchemaType = tmp.SchemaType
sd.References = tmp.References
sd.Subject = tmp.Subject
return err
}
// SchemaMetadata represents schema metadata
type SchemaMetadata struct {
SchemaInfo
ID int `json:"id,omitempty"`
Subject string `json:"subject,omitempty"`
Version int `json:"version,omitempty"`
}
// MarshalJSON implements the json.Marshaler interface
func (sd *SchemaMetadata) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Schema string `json:"schema,omitempty"`
SchemaType string `json:"schemaType,omitempty"`
References []Reference `json:"references,omitempty"`
ID int `json:"id,omitempty"`
Subject string `json:"subject,omitempty"`
Version int `json:"version,omitempty"`
}{
sd.Schema,
sd.SchemaType,
sd.References,
sd.ID,
sd.Subject,
sd.Version,
})
}
// UnmarshalJSON implements the json.Unmarshaller interface
func (sd *SchemaMetadata) UnmarshalJSON(b []byte) error {
var err error
var tmp struct {
Schema string `json:"schema,omitempty"`
SchemaType string `json:"schemaType,omitempty"`
References []Reference `json:"references,omitempty"`
ID int `json:"id,omitempty"`
Subject string `json:"subject,omitempty"`
Version int `json:"version,omitempty"`
}
err = json.Unmarshal(b, &tmp)
sd.Schema = tmp.Schema
sd.SchemaType = tmp.SchemaType
sd.References = tmp.References
sd.ID = tmp.ID
sd.Subject = tmp.Subject
sd.Version = tmp.Version
return err
}
type subjectJSON struct {
subject string
json string
}
type subjectID struct {
subject string
id int
}
type subjectVersion struct {
subject string
version int
}
type subjectOnlyID struct {
id int
}
/* HTTP(S) Schema Registry Client and schema caches */
type client struct {
sync.Mutex
restService *restService
schemaToIdCache cache.Cache
schemaToIdCacheLock sync.RWMutex
idToSchemaCache cache.Cache
idToSchemaCacheLock sync.RWMutex
schemaToVersionCache cache.Cache
schemaToVersionCacheLock sync.RWMutex
versionToSchemaCache cache.Cache
versionToSchemaCacheLock sync.RWMutex
}
var _ Client = new(client)
// Client is an interface for clients interacting with the Confluent Schema Registry.
// The Schema Registry's REST interface is further explained in Confluent's Schema Registry API documentation
// https://github.com/confluentinc/schema-registry/blob/master/client/src/main/java/io/confluent/kafka/schemaregistry/client/SchemaRegistryClient.java
type Client interface {
Register(subject string, schema SchemaInfo, normalize bool) (id int, fromSR int, err error)
GetByID(id, fromSR int) (schema SchemaInfo, err error)
GetBySubjectAndID(subject string, id int) (schema SchemaInfo, err error)
GetID(subject string, schema SchemaInfo, normalize bool) (id int, err error)
GetLatestSchemaMetadata(subject string) (SchemaMetadata, error)
GetSchemaMetadata(subject string, version int) (SchemaMetadata, error)
GetAllVersions(subject string) ([]int, error)
GetVersion(subject string, schema SchemaInfo, normalize bool) (version int, err error)
GetAllSubjects() ([]string, error)
DeleteSubject(subject string, permanent bool) ([]int, error)
DeleteSubjectVersion(subject string, version int, permanent bool) (deletes int, err error)
GetCompatibility(subject string) (compatibility Compatibility, err error)
UpdateCompatibility(subject string, update Compatibility) (compatibility Compatibility, err error)
TestCompatibility(subject string, version int, schema SchemaInfo) (compatible bool, err error)
GetDefaultCompatibility() (compatibility Compatibility, err error)
UpdateDefaultCompatibility(update Compatibility) (compatibility Compatibility, err error)
}
// NewClient returns a Client implementation
func NewClient(conf *Config) (Client, error) {
urlConf := conf.SchemaRegistryURL
if strings.HasPrefix(urlConf, "mock://") {
url, err := url.Parse(urlConf)
if err != nil {
return nil, err
}
mock := &mockclient{
url: url,
schemaToIdCache: make(map[subjectJSON]idCacheEntry),
idToSchemaCache: make(map[subjectID]*SchemaInfo),
schemaToVersionCache: make(map[subjectJSON]versionCacheEntry),
compatibilityCache: make(map[string]Compatibility),
}
return mock, nil
}
restService, err := newRestService(conf)
if err != nil {
return nil, err
}
var schemaToIdCache cache.Cache
var idToSchemaCache cache.Cache
var schemaToVersionCache cache.Cache
var versionToSchemaCache cache.Cache
if conf.CacheCapacity != 0 {
schemaToIdCache, err = cache.NewLRUCache(conf.CacheCapacity)
if err != nil {
return nil, err
}
idToSchemaCache, err = cache.NewLRUCache(conf.CacheCapacity)
if err != nil {
return nil, err
}
schemaToVersionCache, err = cache.NewLRUCache(conf.CacheCapacity)
if err != nil {
return nil, err
}
versionToSchemaCache, err = cache.NewLRUCache(conf.CacheCapacity)
if err != nil {
return nil, err
}
} else {
schemaToIdCache = cache.NewMapCache()
idToSchemaCache = cache.NewMapCache()
schemaToVersionCache = cache.NewMapCache()
versionToSchemaCache = cache.NewMapCache()
}
handle := &client{
restService: restService,
schemaToIdCache: schemaToIdCache,
idToSchemaCache: idToSchemaCache,
schemaToVersionCache: schemaToVersionCache,
versionToSchemaCache: versionToSchemaCache,
}
return handle, nil
}
// Register registers Schema aliased with subject
func (c *client) Register(subject string, schema SchemaInfo, normalize bool) (id int, fromSR int, err error) {
schemaJSON, err := schema.MarshalJSON()
if err != nil {
return -1, 0, err
}
cacheKey := subjectJSON{
subject: subject,
json: string(schemaJSON),
}
c.schemaToIdCacheLock.RLock()
idValue, ok := c.schemaToIdCache.Get(cacheKey)
c.schemaToIdCacheLock.RUnlock()
if ok {
return idValue.(int), 0, nil
}
metadata := SchemaMetadata{
SchemaInfo: schema,
}
c.schemaToIdCacheLock.Lock()
// another goroutine could have already put it in cache
idValue, ok = c.schemaToIdCache.Get(cacheKey)
if !ok {
err = c.restService.handleRequest(newRequest("POST", versionNormalize, &metadata, url.PathEscape(subject), normalize), &metadata)
if err == nil {
c.schemaToIdCache.Put(cacheKey, metadata.ID)
// save the subject matching the ID
// sip := subjectIDPayload{Subject: subject}
// subjID := subjectID{metadata.ID}
// c.schemaToIdCache.Put(subjID, sip)
} else {
metadata.ID = -1
}
fromSR = 1
} else {
metadata.ID = idValue.(int)
fromSR = 0
}
c.schemaToIdCacheLock.Unlock()
return metadata.ID, fromSR, err
}
func (c *client) reqSchemaRegistryMetadataFromID(id int, cacheKey subjectOnlyID, newInfo *SchemaInfo) (err error) {
metadata := SchemaMetadata{}
err = c.restService.handleRequest(newRequest("GET", schemas, nil, id), &metadata)
if err == nil {
newInfo.Schema = metadata.Schema
newInfo.SchemaType = metadata.SchemaType
newInfo.References = metadata.References
// get the schema subject matching the schema id
var response []string
err = c.restService.handleRequest(newRequest("GET", getSubject, nil, id), &response)
if err == nil {
newInfo.Subject = response
return err
} else {
return fmt.Errorf("Invalid server error")
}
} else {
return fmt.Errorf("Invalid server error")
}
}
// GetByID returns the schema identified by id
// Returns Schema object on success
func (c *client) GetByID(id, fromSR int) (schema SchemaInfo, err error) {
cacheKey := subjectOnlyID{id}
newInfo := &SchemaInfo{}
// if producer got the schemaID from the SR, update the cache
if fromSR == 1 {
c.idToSchemaCacheLock.Lock()
err = c.reqSchemaRegistryMetadataFromID(id, cacheKey, newInfo)
if err != nil {
return *newInfo, err
}
c.idToSchemaCache.Put(cacheKey, newInfo)
c.idToSchemaCacheLock.Unlock()
return *newInfo, err
}
c.idToSchemaCacheLock.RLock()
subjIDPayload, ok := c.idToSchemaCache.Get(cacheKey)
c.idToSchemaCacheLock.RUnlock()
if ok {
return *subjIDPayload.(*SchemaInfo), nil
}
c.idToSchemaCacheLock.Lock()
// another goroutine could have already put it in cache
subjIDPayload, ok = c.idToSchemaCache.Get(cacheKey)
if !ok {
err = c.reqSchemaRegistryMetadataFromID(id, cacheKey, newInfo)
if err != nil {
c.idToSchemaCacheLock.Unlock()
return *newInfo, err
}
c.idToSchemaCache.Put(cacheKey, newInfo)
} else {
newInfo = subjIDPayload.(*SchemaInfo)
}
c.idToSchemaCacheLock.Unlock()
return *newInfo, err
}
// GetBySubjectAndID returns the schema identified by id
// Returns Schema object on success
func (c *client) GetBySubjectAndID(subject string, id int) (schema SchemaInfo, err error) {
cacheKey := subjectID{
subject: subject,
id: id,
}
c.idToSchemaCacheLock.RLock()
infoValue, ok := c.idToSchemaCache.Get(cacheKey)
c.idToSchemaCacheLock.RUnlock()
if ok {
return *infoValue.(*SchemaInfo), nil
}
metadata := SchemaMetadata{}
newInfo := &SchemaInfo{}
c.idToSchemaCacheLock.Lock()
// another goroutine could have already put it in cache
infoValue, ok = c.idToSchemaCache.Get(cacheKey)
if !ok {
if len(subject) > 0 {
err = c.restService.handleRequest(newRequest("GET", schemasBySubject, nil, id, url.QueryEscape(subject)), &metadata)
} else {
// NOTE kind of useless... maybe remove that
err = c.restService.handleRequest(newRequest("GET", schemas, nil, id), &metadata)
}
if err == nil {
newInfo.Schema = metadata.Schema
newInfo.SchemaType = metadata.SchemaType
newInfo.References = metadata.References
c.idToSchemaCache.Put(cacheKey, newInfo)
}
} else {
newInfo = infoValue.(*SchemaInfo)
}
c.idToSchemaCacheLock.Unlock()
return *newInfo, err
}
// GetID checks if a schema has been registered with the subject. Returns ID if the registration can be found
func (c *client) GetID(subject string, schema SchemaInfo, normalize bool) (id int, err error) {
schemaJSON, err := schema.MarshalJSON()
if err != nil {
return -1, err
}
cacheKey := subjectJSON{
subject: subject,
json: string(schemaJSON),
}
c.schemaToIdCacheLock.RLock()
idValue, ok := c.schemaToIdCache.Get(cacheKey)
c.schemaToIdCacheLock.RUnlock()
if ok {
return idValue.(int), nil
}
metadata := SchemaMetadata{
SchemaInfo: schema,
}
c.schemaToIdCacheLock.Lock()
// another goroutine could have already put it in cache
idValue, ok = c.schemaToIdCache.Get(cacheKey)
if !ok {
err = c.restService.handleRequest(newRequest("POST", subjectsNormalize, &metadata, url.PathEscape(subject), normalize), &metadata)
if err == nil {
c.schemaToIdCache.Put(cacheKey, metadata.ID)
} else {
metadata.ID = -1
}
} else {
metadata.ID = idValue.(int)
}
c.schemaToIdCacheLock.Unlock()
return metadata.ID, err
}
// GetLatestSchemaMetadata fetches latest version registered with the provided subject
// Returns SchemaMetadata object
func (c *client) GetLatestSchemaMetadata(subject string) (result SchemaMetadata, err error) {
err = c.restService.handleRequest(newRequest("GET", versions, nil, url.PathEscape(subject), "latest"), &result)
return result, err
}
// GetSchemaMetadata fetches the requested subject schema identified by version
// Returns SchemaMetadata object
func (c *client) GetSchemaMetadata(subject string, version int) (result SchemaMetadata, err error) {
cacheKey := subjectVersion{
subject: subject,
version: version,
}
c.versionToSchemaCacheLock.RLock()
metadataValue, ok := c.versionToSchemaCache.Get(cacheKey)
c.versionToSchemaCacheLock.RUnlock()
if ok {
return *metadataValue.(*SchemaMetadata), nil
}
c.versionToSchemaCacheLock.Lock()
// another goroutine could have already put it in cache
metadataValue, ok = c.versionToSchemaCache.Get(cacheKey)
if !ok {
err = c.restService.handleRequest(newRequest("GET", versions, nil, url.PathEscape(subject), version), &result)
if err == nil {
c.versionToSchemaCache.Put(cacheKey, &result)
}
} else {
result = *metadataValue.(*SchemaMetadata)
}
c.versionToSchemaCacheLock.Unlock()
return result, err
}
// GetAllVersions fetches a list of all version numbers associated with the provided subject registration
// Returns integer slice on success
func (c *client) GetAllVersions(subject string) (results []int, err error) {
var result []int
err = c.restService.handleRequest(newRequest("GET", version, nil, url.PathEscape(subject)), &result)
return result, err
}
// GetVersion finds the Subject SchemaMetadata associated with the provided schema
// Returns integer SchemaMetadata number
func (c *client) GetVersion(subject string, schema SchemaInfo, normalize bool) (version int, err error) {
schemaJSON, err := schema.MarshalJSON()
if err != nil {
return -1, err
}
cacheKey := subjectJSON{
subject: subject,
json: string(schemaJSON),
}
c.schemaToVersionCacheLock.RLock()
versionValue, ok := c.schemaToVersionCache.Get(cacheKey)
c.schemaToVersionCacheLock.RUnlock()
if ok {
return versionValue.(int), nil
}
metadata := SchemaMetadata{
SchemaInfo: schema,
}
c.schemaToVersionCacheLock.Lock()
// another goroutine could have already put it in cache
versionValue, ok = c.schemaToVersionCache.Get(cacheKey)
if !ok {
err = c.restService.handleRequest(newRequest("POST", subjectsNormalize, &metadata, url.PathEscape(subject), normalize), &metadata)
if err == nil {
c.schemaToVersionCache.Put(cacheKey, metadata.Version)
} else {
metadata.Version = -1
}
} else {
metadata.Version = versionValue.(int)
}
c.schemaToVersionCacheLock.Unlock()
return metadata.Version, err
}
// Fetch all Subjects registered with the schema Registry
// Returns a string slice containing all registered subjects
func (c *client) GetAllSubjects() ([]string, error) {
var result []string
err := c.restService.handleRequest(newRequest("GET", subject, nil), &result)
return result, err
}
// Deletes provided Subject from registry
// Returns integer slice of versions removed by delete
func (c *client) DeleteSubject(subject string, permanent bool) (deleted []int, err error) {
c.schemaToIdCacheLock.Lock()
for keyValue := range c.schemaToIdCache.ToMap() {
key := keyValue.(subjectJSON)
if key.subject == subject {
c.schemaToIdCache.Delete(key)
}
}
c.schemaToIdCacheLock.Unlock()
c.schemaToVersionCacheLock.Lock()
for keyValue := range c.schemaToVersionCache.ToMap() {
key := keyValue.(subjectJSON)
if key.subject == subject {
c.schemaToVersionCache.Delete(key)
}
}
c.schemaToVersionCacheLock.Unlock()
c.versionToSchemaCacheLock.Lock()
for keyValue := range c.versionToSchemaCache.ToMap() {
key := keyValue.(subjectVersion)
if key.subject == subject {
c.versionToSchemaCache.Delete(key)
}
}
c.versionToSchemaCacheLock.Unlock()
c.idToSchemaCacheLock.Lock()
for keyValue := range c.idToSchemaCache.ToMap() {
key := keyValue.(subjectID)
if key.subject == subject {
c.idToSchemaCache.Delete(key)
}
}
c.idToSchemaCacheLock.Unlock()
var result []int
err = c.restService.handleRequest(newRequest("DELETE", subjectsDelete, nil, url.PathEscape(subject), permanent), &result)
return result, err
}
// DeleteSubjectVersion removes the version identified by delete from the subject's registration
// Returns integer id for the deleted version
func (c *client) DeleteSubjectVersion(subject string, version int, permanent bool) (deleted int, err error) {
c.schemaToVersionCacheLock.Lock()
for keyValue, value := range c.schemaToVersionCache.ToMap() {
key := keyValue.(subjectJSON)
if key.subject == subject && value == version {
c.schemaToVersionCache.Delete(key)
schemaJSON := key.json
cacheKeySchema := subjectJSON{
subject: subject,
json: string(schemaJSON),
}
c.schemaToIdCacheLock.Lock()
idValue, ok := c.schemaToIdCache.Get(cacheKeySchema)
if ok {
c.schemaToIdCache.Delete(cacheKeySchema)
}
c.schemaToIdCacheLock.Unlock()
if ok {
id := idValue.(int)
c.idToSchemaCacheLock.Lock()
cacheKeyID := subjectID{
subject: subject,
id: id,
}
c.idToSchemaCache.Delete(cacheKeyID)
c.idToSchemaCacheLock.Unlock()
}
}
}
c.schemaToVersionCacheLock.Unlock()
c.versionToSchemaCacheLock.Lock()
cacheKey := subjectVersion{
subject: subject,
version: version,
}
c.versionToSchemaCache.Delete(cacheKey)
c.versionToSchemaCacheLock.Unlock()
var result int
err = c.restService.handleRequest(newRequest("DELETE", versionsDelete, nil, url.PathEscape(subject), version, permanent), &result)
return result, err
}
// Compatibility options
type Compatibility int
const (
_ = iota
// None is no compatibility
None
// Backward compatibility
Backward
// Forward compatibility
Forward
// Full compatibility
Full
// BackwardTransitive compatibility
BackwardTransitive
// ForwardTransitive compatibility
ForwardTransitive
// FullTransitive compatibility
FullTransitive
)
var compatibilityEnum = []string{
"",
"NONE",
"BACKWARD",
"FORWARD",
"FULL",
"BACKWARD_TRANSITIVE",
"FORWARD_TRANSITIVE",
"FULL_TRANSITIVE",
}
/* NOTE: GET uses compatibilityLevel, POST uses compatibility */
type compatibilityLevel struct {
CompatibilityUpdate Compatibility `json:"compatibility,omitempty"`
Compatibility Compatibility `json:"compatibilityLevel,omitempty"`
}
// MarshalJSON implements json.Marshaler
func (c Compatibility) MarshalJSON() ([]byte, error) {
return json.Marshal(c.String())
}
// UnmarshalJSON implements json.Unmarshaler
func (c *Compatibility) UnmarshalJSON(b []byte) error {
val := string(b[1 : len(b)-1])
return c.ParseString(val)
}
type compatibilityValue struct {
Compatible bool `json:"is_compatible,omitempty"`
}
func (c Compatibility) String() string {
return compatibilityEnum[c]
}
// ParseString returns a Compatibility for the given string
func (c *Compatibility) ParseString(val string) error {
for idx, elm := range compatibilityEnum {
if elm == val {
*c = Compatibility(idx)
return nil
}
}
return fmt.Errorf("failed to unmarshal Compatibility")
}
// Fetch compatibility level currently configured for provided subject
// Returns compatibility level string upon success
func (c *client) GetCompatibility(subject string) (compatibility Compatibility, err error) {
var result compatibilityLevel
err = c.restService.handleRequest(newRequest("GET", subjectConfig, nil, url.PathEscape(subject)), &result)
return result.Compatibility, err
}
// UpdateCompatibility updates subject's compatibility level
// Returns new compatibility level string upon success
func (c *client) UpdateCompatibility(subject string, update Compatibility) (compatibility Compatibility, err error) {
result := compatibilityLevel{
CompatibilityUpdate: update,
}
err = c.restService.handleRequest(newRequest("PUT", subjectConfig, &result, url.PathEscape(subject)), &result)
return result.CompatibilityUpdate, err
}
// TestCompatibility verifies schema against the subject's compatibility policy
// Returns true if the schema is compatible, false otherwise
func (c *client) TestCompatibility(subject string, version int, schema SchemaInfo) (ok bool, err error) {
var result compatibilityValue
candidate := SchemaMetadata{
SchemaInfo: schema,
}
err = c.restService.handleRequest(newRequest("POST", compatibility, &candidate, url.PathEscape(subject), version), &result)
return result.Compatible, err
}
// GetDefaultCompatibility fetches the global(default) compatibility level
// Returns global(default) compatibility level
func (c *client) GetDefaultCompatibility() (compatibility Compatibility, err error) {
var result compatibilityLevel
err = c.restService.handleRequest(newRequest("GET", config, nil), &result)
return result.Compatibility, err
}
// UpdateDefaultCompatibility updates the global(default) compatibility level level
// Returns new string compatibility level
func (c *client) UpdateDefaultCompatibility(update Compatibility) (compatibility Compatibility, err error) {
result := compatibilityLevel{
CompatibilityUpdate: update,
}
err = c.restService.handleRequest(newRequest("PUT", config, &result), &result)
return result.CompatibilityUpdate, err
}