-
Notifications
You must be signed in to change notification settings - Fork 592
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
Transforms Rust SDK: Add caching layer to Schema Registry client (LRU) #19859
Transforms Rust SDK: Add caching layer to Schema Registry client (LRU) #19859
Conversation
5462866
to
a090d2d
Compare
@rockwotj - this has been hangin' out for a little bit. back burnered for things with core dependencies. would be interested to hear your thoughts. particularly on the unfortunate reality of making |
I will take a closer look next week. We should do LRU caching, but should consider interior mutability if this design is too invasive: https://doc.rust-lang.org/book/ch15-05-interior-mutability.html |
Ah yeah, that's what i was looking for but didn't find. Nice one, thanks! |
a090d2d
to
00f1bd6
Compare
@@ -33,6 +33,7 @@ use redpanda_transform_sdk_sr_types::*; | |||
use redpanda_transform_sdk_varint as varint; | |||
|
|||
use lru::LruCache; | |||
use std::cell::UnsafeCell; |
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.
ace 🎯
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.
Let's use RefCell here please, I don't think the safety checks are a performance concern (and let's prove it with benchmarks first).
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.
Sure. I will note that I chose unsafecell because I was not aware refcell could also do this (i.e. not for perf reasons). Easy benchmark tho.
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.
Gotcha, no need to benchmark, let's just stick with RefCell for now
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.
This wound up kinda gross. get
operations on the cache return optional borrows, but as I understand it those are bound to the lifetime of the RefMut
that we peel off from RefCell
, so you can't (?) return them (easily?).
I suspect there's a way around it with Ref::map
& friends, but then there's other weird stuff with passing in a RefMut
to get a Ref
out, etc.
Took a copy for now, but open to feedback.
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.
kinda gross
or totally fine. fact remains that I didn't find a common pattern for this exact usage, but we've not introduced any additional clones afaict: #19859 (comment)
472f2b8
to
f453848
Compare
@@ -33,6 +33,7 @@ use redpanda_transform_sdk_sr_types::*; | |||
use redpanda_transform_sdk_varint as varint; | |||
|
|||
use lru::LruCache; | |||
use std::cell::UnsafeCell; |
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.
Let's use RefCell here please, I don't think the safety checks are a performance concern (and let's prove it with benchmarks first).
c9a89db
to
a2aae64
Compare
src/transform-sdk/rust/sr/src/lib.rs
Outdated
impl SchemaRegistryClientImpl for CachingSchemaRegistryClient { | ||
fn lookup_schema_by_id(&self, id: SchemaId) -> Result<Schema> { | ||
if let Some(schema) = self.schema_by_id_cache.get(&id) { | ||
Ok(schema.clone()) |
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.
ah, i guess we're cloning these out anyway. now cache::get will return a value type so we can just relocate into the Result.
a2aae64
to
6a88bbb
Compare
force push contents:
|
} | ||
|
||
fn get(&self, k: &Key) -> Option<Value> { | ||
if let Some(v) = self.underlying.borrow_mut().get(k) { |
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.
Do you need a mutable borrow here?
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.
Yeah, presumably since the cache needs to update it's recency accounting. It's in the API anyway.
src/transform-sdk/rust/sr/src/lib.rs
Outdated
underlying: RefCell<LruCacheImpl<K, V>>, | ||
} | ||
|
||
#[allow(dead_code)] |
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.
Where is the dead code? Is this because we always pass max_entries
as None? if that's so can we either fix that or move the scope of this allow to that function?
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.
added this to silence a warning in the commit that introduced the struct. removed now at first usage.
For convenience during schema-based record serde. Also deprecates the slightly clunkier extract_id and migrates the SR/rust example transform to the new version. Includes simple roundtrip unit tests. Signed-off-by: Oren Leiman <oren.leiman@redpanda.com>
Signed-off-by: Oren Leiman <oren.leiman@redpanda.com>
Signed-off-by: Oren Leiman <oren.leiman@redpanda.com>
The idea here is to wrap an RefCell<LruCache<>> so that we can have interior mutability for cache operations Signed-off-by: Oren Leiman <oren.leiman@redpanda.com>
Encapsulates a non-caching client impl AND LruCaches keyed on SchemaId and SubjectVersion. Signed-off-by: Oren Leiman <oren.leiman@redpanda.com>
Signed-off-by: Oren Leiman <oren.leiman@redpanda.com>
6a88bbb
to
d74df0f
Compare
force push CR changes |
.
Backports Required
Release Notes
Improvements