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
//! This module exposes three fundamental concepts:
//!
//! [PbBufferReader]
//! A [PbBufferReader] is something which a [Message] can be deserialized from. In the common case,
//! this means that the relevant bytes are copied out of the underlying store and copied into an
//! appropriate struct which implements [Message]. The underlying data store is referred to as a
//! [PbBuffer].
//!
//! [PbBufferWriter]
//! A [PbBufferWriter] is something which a [Message] can be serialized to. In the common case, this
//! means that the relevant bytes are copied out of the concrete struct and into the underlying
//! data store. The underlying data store is referred to as a [PbBuffer].
//!
//! [Lazy]
//! The interesting trick of the [PbBuffer]/[PbBufferReader]/[PbBufferWriter] abstraction is that there are
//! cases where we want to minimize the number of times we copy the data contained within a message
//! (or field of a message). Especially on resource-constrained hardware (mostly MP OSDs), we want
//! to be able to avoid the cost of serialization and deserialization.
//!
//! To support this, a [PbBufferReader] and a [PbBufferWriter] may use *compatible* backing stores, and
//! expose a [Lazy] message type which represents the not-yet-deserialized data. The flow is
//! roughly as follows:
//!
//! Request (bytes on the wire)
//! |
//! v
//! RPC Framework (with an underlying allocator, e.g. blob::Blob or grpc::Slice)
//! |
//! v
//! BR: PbBufferReader deserializes the struct using RPC framework's allocator.
//! |
//! v
//! Request (concrete struct containing a [Lazy] field)
//! |
//! v
//! RPC handler (doesn't modify the [Lazy] field)
//! |
//! v
//! Response (concrete struct containing a [Lazy] field)
//! |
//! v
//! BW: PbBufferWriter serializes the struct using RPC framework's allocator.
//! |
//! v
//! RPC Framework (with an underlying allocator, e.g. blob::Blob or grpc::Slice)
//! |
//! v
//! Response (bytes on the wire).
//!
//! In order for [Lazy] to work (i.e. minimize copies), we must have the [PbBufferReader] and the
//! [PbBufferWriter] use compatible underlying allocators. Compatibility is defined by the
//! both of the following:
//! - the ability for the [PbBufferWriter] to successfully call `write_buffer` for the appropriate
//! [PbBuffer] type, which writes non-deserialized data back into the data store.
//! - the ability to use [PbBuffer::into_reader] and [PbBufferReader::as_buffer] to *convert* between
//! underlying data stores.
//!
//! Roughly speaking, [Lazy] converts to the appropriate [PbBuffer] type when `deserialize` is
//! called, and then lazily writes itself when `serialize` is called.
//!
//! In the status quo, the behavior is as follows:
//!
//! `blob_pb::WrappedBlob` and `blob_pb::VecSlice` allow zero-copy deserialization -> serialization,
//! provided that their respective [PbBufferWriter]s are used.
//! Converting from `blob_pb::WrappedBlob` to a `blob_pb::VecSlice` is zero-copy.
//! Converting from a `blob_pb::VecSlice` to a `blob_pb::Blob` requires a single copy.
use std::any::{
type_name,
Any,
};
use std::default::Default;
use std::fmt::{
self,
Debug,
};
use std::io::{
Cursor,
Error,
ErrorKind,
Result,
Write,
};
use bytes::{
Buf,
Bytes,
};
use super::Message;
/// A stand-in trait for any backing buffer store. Required to be object-safe for lazy evaluation.
/// PbBuffers are expected to own references to the data they reference, and should be cheap
/// (constant-time) to clone.
#[allow(clippy::len_without_is_empty)]
pub trait PbBuffer: Any + Clone + Default {
type Reader: PbBufferReader;
fn len(&self) -> usize;
fn into_reader(self) -> Self::Reader;
/// Deserialize this buffer from a reader. Unless overridden, this will error
/// if the reader does not support casting to [Self].
fn from_reader<R: PbBufferReader>(reader: &mut R) -> Result<Self> {
reader.as_buffer::<Self>()
}
}
/// If `B1` and `B2` are the same type, returns the passed-in buffer;
/// otherwise, returns an error.
pub fn cast_buffer<B1: PbBuffer, B2: PbBuffer>(buf: B1) -> Result<B2> {
let mut buf = Some(buf);
if let Some(buf) = (&mut buf as &mut dyn Any).downcast_mut::<Option<B2>>() {
Ok(buf.take().unwrap())
} else {
Err(Error::new(
ErrorKind::InvalidInput,
format!(
"This reader produces buffers of type {}, not {}",
type_name::<B1>(),
type_name::<B2>()
),
))
}
}
/// All concrete types which are used for deserialization should implement
/// [PbBufferReader], which includes functions to convert to and from [PbBuffer].
pub trait PbBufferReader: Buf + Sized {
/// Get a reference to the underlying [PbBuffer]. This is expected to be cheap,
/// if supported, or return error.
/// The implementation should dispatch on the type `B` and return an error
/// if the requested buffer type is unknown.
fn as_buffer<B: PbBuffer>(&self) -> Result<B> {
Err(Error::new(
ErrorKind::InvalidInput,
"Taking ownership of the PbBuffer from this reader is not supported",
))
}
/// Advance the interal cursor by `at`, and return a [PbBufferReader] corresponding to the
/// traversed indices (i.e. self.position..self.position + at).
fn split(&mut self, at: usize) -> Self;
}
/// All concrete types used for serialization should implement [PbBufferWriter] in order to support
/// serializing lazily-evaluated types without copies.
pub trait PbBufferWriter: Write {
/// Attempt to write a lazily-evaluated buffer into [Self]. If the underlying [B] is not
/// zero-copy-supported by the [PbBufferWriter], this should read/copy the bytes out from [B].
fn write_buffer<B: PbBuffer>(&mut self, buf: &B) -> Result<()>;
}
/// A wrapper around a lazily-evaluted [PbBufferReader] which implements [Message].
#[derive(Clone, Default, PartialEq)]
pub struct Lazy<B: PbBuffer> {
contents: Option<B>,
}
impl<B: PbBuffer> Lazy<B> {
pub fn new(r: B) -> Self {
Self { contents: Some(r) }
}
pub fn into_buffer(self) -> B {
self.contents.unwrap_or_default()
}
}
impl<B: PbBuffer> Debug for Lazy<B> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Lazy")
.field("contents", &self.contents.as_ref().map(|_| "_"))
.finish()
}
}
impl<B: PbBuffer + PartialEq> Message for Lazy<B> {
fn compute_size(&self) -> usize {
self.contents.as_ref().map(PbBuffer::len).unwrap_or(0)
}
fn compute_grpc_slices_size(&self) -> usize {
self.contents.as_ref().map(PbBuffer::len).unwrap_or(0)
}
fn serialize<W: PbBufferWriter>(&self, w: &mut W) -> Result<()> {
if let Some(ref contents) = self.contents {
w.write_buffer(contents)?;
}
Ok(())
}
fn deserialize<R: PbBufferReader>(&mut self, r: &mut R) -> Result<()> {
self.contents = Some(B::from_reader(r)?);
Ok(())
}
}
impl<'a> PbBufferReader for Cursor<&'a [u8]> {
fn split(&mut self, at: usize) -> Self {
let pos = self.position() as usize;
self.advance(at);
let new_slice = &self.get_ref()[pos..pos + at];
Self::new(new_slice)
}
}
impl PbBuffer for Bytes {
type Reader = Cursor<Bytes>;
fn len(&self) -> usize {
self.len()
}
fn into_reader(self) -> Cursor<Bytes> {
Cursor::new(self)
}
}
impl PbBufferReader for Cursor<Bytes> {
fn as_buffer<B: PbBuffer>(&self) -> Result<B> {
let bytes = self.get_ref().slice((self.position() as usize)..);
cast_buffer(bytes)
}
fn split(&mut self, at: usize) -> Self {
let pos = self.position() as usize;
self.advance(at);
let new_slice = self.get_ref().slice(pos..(pos + at));
Self::new(new_slice)
}
}
impl<'a> PbBufferWriter for Cursor<&'a mut Vec<u8>> {
/// Note: this implementation freely copies the data out of `buf`.
#[inline]
fn write_buffer<B: PbBuffer>(&mut self, buf: &B) -> Result<()> {
let mut reader = buf.clone().into_reader();
while reader.has_remaining() {
let n = {
let bytes = reader.chunk();
self.write_all(bytes)?;
bytes.len()
};
reader.advance(n);
}
Ok(())
}
}
impl<'a> PbBufferWriter for Cursor<&'a mut [u8]> {
/// Note: this implementation freely copies the data out of `buf`.
#[inline]
fn write_buffer<B: PbBuffer>(&mut self, buf: &B) -> Result<()> {
let mut reader = buf.clone().into_reader();
while reader.has_remaining() {
let n = {
let bytes = reader.chunk();
self.write_all(bytes)?;
bytes.len()
};
reader.advance(n);
}
Ok(())
}
}
/// A wrapper around a [Write] which copies all the data into the underlying [Write]r.
pub struct CopyWriter<'a, W: Write> {
pub writer: &'a mut W,
}
impl<'a, W: Write + 'a> Write for CopyWriter<'a, W> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.writer.write(buf)
}
#[inline]
fn flush(&mut self) -> Result<()> {
self.writer.flush()
}
}
impl<'a, W: Write + 'a> PbBufferWriter for CopyWriter<'a, W> {
/// Note: this implementation freely copies the data out of `buf`.
#[inline]
fn write_buffer<B: PbBuffer>(&mut self, buf: &B) -> Result<()> {
let mut reader = buf.clone().into_reader();
while reader.has_remaining() {
let n = {
let bytes = reader.chunk();
self.write_all(bytes)?;
bytes.len()
};
reader.advance(n);
}
Ok(())
}
}