-
Notifications
You must be signed in to change notification settings - Fork 636
feat(sink): support encode protobuf
with confluent schema registry
#15546
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use bytes::Bytes; | ||
use bytes::{BufMut, Bytes}; | ||
use prost::Message; | ||
use prost_reflect::{ | ||
DynamicMessage, FieldDescriptor, Kind, MessageDescriptor, ReflectMessage, Value, | ||
|
@@ -30,13 +30,25 @@ pub struct ProtoEncoder { | |
schema: Schema, | ||
col_indices: Option<Vec<usize>>, | ||
descriptor: MessageDescriptor, | ||
header: ProtoHeader, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum ProtoHeader { | ||
None, | ||
/// <https://docs.confluent.io/platform/7.5/schema-registry/fundamentals/serdes-develop/index.html#messages-wire-format> | ||
/// | ||
/// * 00 | ||
/// * 4-byte big-endian schema ID | ||
ConfluentSchemaRegistry(i32), | ||
} | ||
|
||
impl ProtoEncoder { | ||
pub fn new( | ||
schema: Schema, | ||
col_indices: Option<Vec<usize>>, | ||
descriptor: MessageDescriptor, | ||
header: ProtoHeader, | ||
) -> SinkResult<Self> { | ||
match &col_indices { | ||
Some(col_indices) => validate_fields( | ||
|
@@ -59,12 +71,18 @@ impl ProtoEncoder { | |
schema, | ||
col_indices, | ||
descriptor, | ||
header, | ||
}) | ||
} | ||
} | ||
|
||
pub struct ProtoEncoded { | ||
message: DynamicMessage, | ||
header: ProtoHeader, | ||
} | ||
|
||
impl RowEncoder for ProtoEncoder { | ||
type Output = DynamicMessage; | ||
type Output = ProtoEncoded; | ||
|
||
fn schema(&self) -> &Schema { | ||
&self.schema | ||
|
@@ -87,12 +105,68 @@ impl RowEncoder for ProtoEncoder { | |
&self.descriptor, | ||
) | ||
.map_err(Into::into) | ||
.map(|m| ProtoEncoded { | ||
message: m, | ||
header: self.header, | ||
}) | ||
} | ||
} | ||
|
||
impl SerTo<Vec<u8>> for DynamicMessage { | ||
impl SerTo<Vec<u8>> for ProtoEncoded { | ||
fn ser_to(self) -> SinkResult<Vec<u8>> { | ||
Ok(self.encode_to_vec()) | ||
let mut buf = Vec::new(); | ||
match self.header { | ||
ProtoHeader::None => { /* noop */ } | ||
ProtoHeader::ConfluentSchemaRegistry(schema_id) => { | ||
buf.reserve(1 + 4); | ||
buf.put_u8(0); | ||
buf.put_i32(schema_id); | ||
MessageIndexes::from(self.message.descriptor()).encode(&mut buf); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be pre-computed and is same for all messages. |
||
} | ||
} | ||
self.message.encode(&mut buf).unwrap(); | ||
Ok(buf) | ||
} | ||
} | ||
|
||
struct MessageIndexes(Vec<i32>); | ||
|
||
impl MessageIndexes { | ||
fn from(desc: MessageDescriptor) -> Self { | ||
// https://github.com/protocolbuffers/protobuf/blob/v25.1/src/google/protobuf/descriptor.proto | ||
// https://docs.rs/prost-reflect/0.12.0/src/prost_reflect/descriptor/tag.rs.html | ||
// https://docs.rs/prost-reflect/0.12.0/src/prost_reflect/descriptor/build/visit.rs.html#125 | ||
// `FileDescriptorProto` field #4 is `repeated DescriptorProto message_type` | ||
const TAG_FILE_MESSAGE: i32 = 4; | ||
// `DescriptorProto` field #3 is `repeated DescriptorProto nested_type` | ||
const TAG_MESSAGE_NESTED: i32 = 3; | ||
|
||
let mut indexes = vec![]; | ||
let mut path = desc.path().array_chunks(); | ||
let &[tag, idx] = path.next().unwrap(); | ||
assert_eq!(tag, TAG_FILE_MESSAGE); | ||
indexes.push(idx); | ||
for &[tag, idx] in path { | ||
assert_eq!(tag, TAG_MESSAGE_NESTED); | ||
indexes.push(idx); | ||
} | ||
Self(indexes) | ||
} | ||
|
||
fn zig_i32(value: i32, buf: &mut impl BufMut) { | ||
let unsigned = ((value << 1) ^ (value >> 31)) as u32 as u64; | ||
prost::encoding::encode_varint(unsigned, buf); | ||
} | ||
|
||
fn encode(&self, buf: &mut impl BufMut) { | ||
if self.0 == [0] { | ||
buf.put_u8(0); | ||
return; | ||
} | ||
Self::zig_i32(self.0.len().try_into().unwrap(), buf); | ||
for &idx in &self.0 { | ||
Self::zig_i32(idx, buf); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -367,7 +441,8 @@ mod tests { | |
Some(ScalarImpl::Timestamptz(Timestamptz::from_micros(3))), | ||
]); | ||
|
||
let encoder = ProtoEncoder::new(schema, None, descriptor.clone()).unwrap(); | ||
let encoder = | ||
ProtoEncoder::new(schema, None, descriptor.clone(), ProtoHeader::None).unwrap(); | ||
let m = encoder.encode(row).unwrap(); | ||
let encoded: Vec<u8> = m.ser_to().unwrap(); | ||
assert_eq!( | ||
|
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.
do we also need to handle the auth for schema registry 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.
It is handled inside the unified
SchemaLoader
(#14642)risingwave/src/connector/src/schema/loader.rs
Line 46 in ef7eb1d
But yes our e2e tests use redpanda without password, and cannot catch bugs on auth (e.g. #14755).
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.
Seems to be an involved process. Leaving it as a separate issue.
https://docs.redpanda.com/23.2/manage/security/authentication/#configure-basic-authentication