-
Notifications
You must be signed in to change notification settings - Fork 603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
transform-sdk: Introduce sr package #13049
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1d94216
Add the ABI for exposing sr functionality in the broker to Wasm
rockwotj 1f9b387
Add encoding/decoding for the schema registry ABI
rockwotj 133187d
Add a schema registry client to the transform-sdk
rockwotj 31353e7
Add module documentation for `sr`
rockwotj a225b32
Add a serde helper for encoding/decoding schema registry encoded data.
rockwotj 7a683a0
Add an example of the schema registry for internal tests
rockwotj 673642c
wasm: Add a fake schema registry for unit tests
rockwotj 1bd980d
wasm: Add a test for schema registry functionality in Transforms
rockwotj 6838063
Give each module it's own gopath
rockwotj fb7bd16
Add -modcacherw to tinygo builds
rockwotj e814403
Add a simple number of entries based cache for the schema registry
rockwotj 2733c83
Prevent cache from temporarily being n+1 in size
rockwotj 66f2794
Clarify eviction policy in cache
rockwotj 71b9745
Add a small LRU queue with a linked list
rockwotj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cache | ||
|
||
import "container/list" | ||
|
||
type ( | ||
entry[K comparable, V any] struct { | ||
key K | ||
value V | ||
} | ||
// A cache that evicts in LRU order based on number of entries | ||
Cache[K comparable, V any] struct { | ||
// list contains the entries themselves | ||
// in LRU order | ||
list *list.List | ||
underlying map[K]*list.Element | ||
maxEntries int | ||
} | ||
) | ||
|
||
// New returns a new cache with a limited set of entries. | ||
func New[K comparable, V any](maxSize int) Cache[K, V] { | ||
return Cache[K, V]{ | ||
underlying: make(map[K]*list.Element), | ||
list: list.New(), | ||
maxEntries: maxSize, | ||
} | ||
} | ||
|
||
// Put adds an entry into the cache | ||
func (c *Cache[K, V]) Put(k K, v V) { | ||
if n, ok := c.underlying[k]; ok { | ||
// this key is already in the cache, update the value | ||
// then move it to the end of our list | ||
n.Value.(*entry[K, V]).value = v | ||
c.list.MoveToBack(n) | ||
} else { | ||
// otherwise prune the least recently used entry and | ||
// insert the new value | ||
c.prune() | ||
c.underlying[k] = c.list.PushBack(&entry[K, V]{k, v}) | ||
} | ||
} | ||
|
||
// Get extracts a value from the cache | ||
func (c *Cache[K, V]) Get(k K) (v V, ok bool) { | ||
n, ok := c.underlying[k] | ||
if !ok { | ||
return v, false | ||
} | ||
v = n.Value.(*entry[K, V]).value | ||
// move this node to the end of our list | ||
// to mark it as most recently used | ||
c.list.MoveToBack(n) | ||
return v, true | ||
} | ||
|
||
// Size returns the size of the cache | ||
func (c *Cache[K, V]) Size() int { | ||
return len(c.underlying) | ||
} | ||
|
||
func (c *Cache[K, V]) prune() { | ||
for len(c.underlying) >= c.maxEntries { | ||
toRemove := c.list.Front() | ||
entry := toRemove.Value.(*entry[K, V]) | ||
delete(c.underlying, entry.key) | ||
c.list.Remove(toRemove) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cache_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/redpanda-data/redpanda/src/go/transform-sdk/internal/cache" | ||
) | ||
|
||
func expectValue(t *testing.T, c cache.Cache[int, string], key int) string { | ||
v, ok := c.Get(key) | ||
if !ok { | ||
t.Fatalf("missing value for key %v", key) | ||
} | ||
return v | ||
} | ||
|
||
func TestCacheMaxSize(t *testing.T) { | ||
c := cache.New[int, string](3) | ||
c.Put(1, "foo") | ||
c.Put(2, "bar") | ||
c.Put(3, "qux") | ||
c.Put(2, "baz") | ||
if c.Size() != 3 { | ||
t.Fatalf("got: %v want: 3", c.Size()) | ||
} | ||
v := expectValue(t, c, 1) | ||
if v != "foo" { | ||
t.Fatalf("got: %v want: %v", v, "foo") | ||
} | ||
v = expectValue(t, c, 2) | ||
if v != "baz" { | ||
t.Fatalf("got: %v want: %v", v, "baz") | ||
} | ||
v = expectValue(t, c, 3) | ||
if v != "qux" { | ||
t.Fatalf("got: %v want: %v", v, "qux") | ||
} | ||
c.Put(4, "thud") | ||
if c.Size() != 3 { | ||
t.Fatalf("got: %v want: 3", c.Size()) | ||
} | ||
if _, ok := c.Get(1); ok { | ||
t.Fatalf("expected evicted entry for %v", 1) | ||
} | ||
v = expectValue(t, c, 2) | ||
if v != "baz" { | ||
t.Fatalf("got: %v want: %v", v, "baz") | ||
} | ||
v = expectValue(t, c, 3) | ||
if v != "qux" { | ||
t.Fatalf("got: %v want: %v", v, "qux") | ||
} | ||
v = expectValue(t, c, 4) | ||
if v != "thud" { | ||
t.Fatalf("got: %v want: %v", v, "thud") | ||
} | ||
} | ||
|
||
func TestCacheLRU(t *testing.T) { | ||
c := cache.New[int, string](3) | ||
c.Put(1, "foo") | ||
c.Put(2, "bar") | ||
c.Put(3, "qux") | ||
v := expectValue(t, c, 1) | ||
if v != "foo" { | ||
t.Fatalf("got: %v want: %v", v, "baz") | ||
} | ||
// this should evict key 2 because it's LRU | ||
c.Put(4, "baz") | ||
if c.Size() != 3 { | ||
t.Fatalf("got: %v want: 3", c.Size()) | ||
} | ||
if _, ok := c.Get(2); ok { | ||
t.Fatalf("got: %v want: false", ok) | ||
} | ||
v = expectValue(t, c, 1) | ||
if v != "foo" { | ||
t.Fatalf("got: %v want: %v", v, "baz") | ||
} | ||
v = expectValue(t, c, 3) | ||
if v != "qux" { | ||
t.Fatalf("got: %v want: %v", v, "qux") | ||
} | ||
v = expectValue(t, c, 4) | ||
if v != "baz" { | ||
t.Fatalf("got: %v want: %v", v, "thud") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/actgardner/gogen-avro/v10 v10.2.1 h1:z3pOGblRjAJCYpkIJ8CmbMJdksi4rAhaygw0dyXZ930= | ||
github.com/actgardner/gogen-avro/v10 v10.2.1/go.mod h1:QUhjeHPchheYmMDni/Nx7VB0RsT/ee8YIgGY/xpEQgQ= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= |
185 changes: 185 additions & 0 deletions
185
src/go/transform-sdk/internal/testdata/schema-registry/avro/example.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
src/go/transform-sdk/internal/testdata/schema-registry/avro/generate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package avro | ||
|
||
//go:generate $GOPATH/bin/gogen-avro --package=avro . schema.avsc |
9 changes: 9 additions & 0 deletions
9
src/go/transform-sdk/internal/testdata/schema-registry/avro/schema.avsc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"type": "record", | ||
"name": "Example", | ||
"namespace": "com.example", | ||
"fields": [ | ||
{"name": "a", "type": "long", "default": 0}, | ||
{"name": "b", "type": "string", "default": ""} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The values (overall) are quite large, so I wonder if it's worth choosing a different data structure that would trade off some memory for bookkeeping, but reduce insertion time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.