From a35e35a10c61e615da3898225ad51f55ca95fa32 Mon Sep 17 00:00:00 2001 From: Jonah Getz Date: Tue, 17 Nov 2026 11:06:33 -0500 Subject: [PATCH 1/4] Prevent events from retaining entire RPC buffers --- build.rs | 26 +++++++++++++++++++++++--- src/proto/vector.rs | 3 +++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index 92ad7f8f0080f..08cfb294fe735 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,21 @@ use std::{collections::HashSet, env, fs::File, io::Write, path::Path, process::Command}; +#[cfg(feature = "protobuf-build")] +struct GrpcServiceGenerator; + +#[cfg(feature = "protobuf-build")] +impl prost_build::ServiceGenerator for GrpcServiceGenerator { + fn generate(&mut self, service: prost_build::Service, buf: &mut String) { + let mut builder = tonic_prost_build::configure(); + if service.package == "vector" && service.name == "Vector" { + builder = builder.codec_path("crate::proto::vector::CopyingProstCodec"); + } + let mut generator = builder.service_generator(); + generator.generate(service, buf); + generator.finalize(buf); + } +} + struct TrackedEnv { tracked: HashSet, } @@ -141,6 +157,8 @@ fn main() { println!("cargo:rerun-if-changed=proto/vector/ddsketch_full.proto"); println!("cargo:rerun-if-changed=proto/vector/vector.proto"); println!("cargo:rerun-if-changed=proto/vector/observability.proto"); + println!("cargo:rerun-if-changed=proto"); + println!("cargo:rerun-if-changed=lib/vector-core/proto"); // Create and store the "file descriptor set" from the compiled Protocol Buffers packages. // Used for gRPC reflection of Vector's own observability/API protos. @@ -154,10 +172,12 @@ fn main() { .file_descriptor_set_path(protobuf_fds_path) .extern_path(".event", "crate::event::proto"); // Use existing event types from vector-core - tonic_prost_build::configure() + // Vector events can outlive their incoming RPC and be split across sink batches. + // Give only that service a copying decoder; keep other gRPC services unchanged. + prost_build + .service_generator(Box::new(GrpcServiceGenerator)) .protoc_arg("--experimental_allow_proto3_optional") - .compile_with_config( - prost_build, + .compile_protos( &[ "lib/vector-core/proto/event.proto", "proto/vector/ddsketch_full.proto", diff --git a/src/proto/vector.rs b/src/proto/vector.rs index 44936cfca46f4..5c00ac66a3501 100644 --- a/src/proto/vector.rs +++ b/src/proto/vector.rs @@ -3,5 +3,8 @@ tonic::include_proto!("vector"); +mod copying_codec; +use copying_codec::CopyingProstCodec; + pub use vector_client::VectorClient as Client; pub use vector_server::{Vector as Service, VectorServer as Server}; From 0acacb59fddc8a11cf68526eaba5428df5b2f482 Mon Sep 17 00:00:00 2001 From: Jonah Getz Date: Tue, 17 Nov 2026 17:32:05 -0500 Subject: [PATCH 2/4] Cleanup --- build.rs | 2 -- src/proto/vector.rs | 3 --- 2 files changed, 5 deletions(-) diff --git a/build.rs b/build.rs index 08cfb294fe735..de04f9aeb1d8e 100644 --- a/build.rs +++ b/build.rs @@ -172,8 +172,6 @@ fn main() { .file_descriptor_set_path(protobuf_fds_path) .extern_path(".event", "crate::event::proto"); // Use existing event types from vector-core - // Vector events can outlive their incoming RPC and be split across sink batches. - // Give only that service a copying decoder; keep other gRPC services unchanged. prost_build .service_generator(Box::new(GrpcServiceGenerator)) .protoc_arg("--experimental_allow_proto3_optional") diff --git a/src/proto/vector.rs b/src/proto/vector.rs index 5c00ac66a3501..44936cfca46f4 100644 --- a/src/proto/vector.rs +++ b/src/proto/vector.rs @@ -3,8 +3,5 @@ tonic::include_proto!("vector"); -mod copying_codec; -use copying_codec::CopyingProstCodec; - pub use vector_client::VectorClient as Client; pub use vector_server::{Vector as Service, VectorServer as Server}; From 4b0074ac6a06825d4e688253dc4d5e6be89089bf Mon Sep 17 00:00:00 2001 From: Jonah Getz Date: Tue, 17 Nov 2026 22:06:01 -0500 Subject: [PATCH 3/4] Copy codec --- src/proto/vector.rs | 3 ++ src/proto/vector/copying_codec.rs | 68 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/proto/vector/copying_codec.rs diff --git a/src/proto/vector.rs b/src/proto/vector.rs index 44936cfca46f4..5c00ac66a3501 100644 --- a/src/proto/vector.rs +++ b/src/proto/vector.rs @@ -3,5 +3,8 @@ tonic::include_proto!("vector"); +mod copying_codec; +use copying_codec::CopyingProstCodec; + pub use vector_client::VectorClient as Client; pub use vector_server::{Vector as Service, VectorServer as Server}; diff --git a/src/proto/vector/copying_codec.rs b/src/proto/vector/copying_codec.rs new file mode 100644 index 0000000000000..14a37ce380135 --- /dev/null +++ b/src/proto/vector/copying_codec.rs @@ -0,0 +1,68 @@ +use std::marker::PhantomData; + +use bytes::Buf; +use prost::Message; +use tonic::{ + Status, + codec::{BufferSettings, Codec, DecodeBuf, Decoder}, +}; +use tonic_prost::ProstCodec; + +/// Preserve independent ownership of fields decoded from Vector RPCs. +/// +/// Tonic 0.12's DecodeBuf shares its backing allocation when Prost decodes Bytes +/// fields. A small surviving event can therefore retain an entire incoming batch, +/// especially when a receiver partitions events into long-lived sink batches. +#[derive(Debug, Default)] +pub(super) struct CopyingProstCodec(PhantomData<(T, U)>); + +impl Codec for CopyingProstCodec +where + T: Message + Send + 'static, + U: Message + Default + Send + 'static, +{ + type Encode = T; + type Decode = U; + type Encoder = as Codec>::Encoder; + type Decoder = CopyingProstDecoder; + + fn encoder(&mut self) -> Self::Encoder { + ProstCodec::::raw_encoder(BufferSettings::default()) + } + + fn decoder(&mut self) -> Self::Decoder { + CopyingProstDecoder(PhantomData) + } +} + +pub(super) struct CopyingProstDecoder(PhantomData); + +impl Decoder for CopyingProstDecoder { + type Item = U; + type Error = Status; + + fn decode(&mut self, buf: &mut DecodeBuf<'_>) -> Result, Status> { + U::decode(CopyingBuf(buf)) + .map(Some) + .map_err(|error| Status::internal(error.to_string())) + } +} + +struct CopyingBuf(B); + +impl Buf for CopyingBuf { + fn remaining(&self) -> usize { + self.0.remaining() + } + + fn chunk(&self) -> &[u8] { + self.0.chunk() + } + + fn advance(&mut self, count: usize) { + self.0.advance(count); + } + + // Deliberately inherit Buf's copying copy_to_bytes implementation, matching + // Tonic 0.11. Forwarding it to DecodeBuf would retain the incoming allocation. +} From 53893d13a534d5d2ab197b33c4751812cdeb32c2 Mon Sep 17 00:00:00 2001 From: Jonah Getz Date: Tue, 24 Nov 2026 10:52:40 -0700 Subject: [PATCH 4/4] Comments --- src/proto/vector/copying_codec.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/proto/vector/copying_codec.rs b/src/proto/vector/copying_codec.rs index 14a37ce380135..26ca5715a1e6b 100644 --- a/src/proto/vector/copying_codec.rs +++ b/src/proto/vector/copying_codec.rs @@ -8,11 +8,6 @@ use tonic::{ }; use tonic_prost::ProstCodec; -/// Preserve independent ownership of fields decoded from Vector RPCs. -/// -/// Tonic 0.12's DecodeBuf shares its backing allocation when Prost decodes Bytes -/// fields. A small surviving event can therefore retain an entire incoming batch, -/// especially when a receiver partitions events into long-lived sink batches. #[derive(Debug, Default)] pub(super) struct CopyingProstCodec(PhantomData<(T, U)>); @@ -63,6 +58,4 @@ impl Buf for CopyingBuf { self.0.advance(count); } - // Deliberately inherit Buf's copying copy_to_bytes implementation, matching - // Tonic 0.11. Forwarding it to DecodeBuf would retain the incoming allocation. }