Skip to main content

snix_castore/proto/
grpc_chunkstore_wrapper.rs

1use crate::{
2    B3Digest,
3    chunkstore::{Chunk, ChunkStore},
4    proto,
5};
6use futures::stream::BoxStream;
7use tonic::{Request, Response, Status, Streaming, async_trait};
8use tracing::{Span, instrument, warn};
9
10/// 2 MiB, well below the size limit of gRPC messages (4 MiB).
11const GRPC_CHUNK_SIZE: usize = 2 * 1024 * 1024;
12
13/// Wraps a [ChunkStore] into a gRPC
14/// [proto::chunk_store_service_server::ChunkStoreService].
15pub struct GRPCChunkStoreWrapper<T> {
16    chunk_store: T,
17}
18
19impl<T> GRPCChunkStoreWrapper<T> {
20    /// Constructs a new [GRPCChunkStoreWrapper] from a [ChunkStore].
21    pub fn new(chunk_store: T) -> Self {
22        Self { chunk_store }
23    }
24}
25
26#[async_trait]
27impl<T> proto::chunk_store_service_server::ChunkStoreService for GRPCChunkStoreWrapper<T>
28where
29    T: ChunkStore + 'static,
30{
31    type GetChunkStream = BoxStream<'static, Result<proto::GetChunkResponse, Status>>;
32
33    #[instrument(skip_all, fields(chunk.digest=tracing::field::Empty))]
34    async fn stat_chunk(
35        &self,
36        request: Request<proto::StatChunkRequest>,
37    ) -> Result<Response<proto::StatChunkResponse>, Status> {
38        let digest: B3Digest = request
39            .into_inner()
40            .digest
41            .try_into()
42            .map_err(|_| Status::invalid_argument("invalid digest length"))?;
43
44        let span = Span::current();
45        span.record("chunk.digest", digest.to_string());
46
47        match self.chunk_store.has(&digest).await {
48            Ok(true) => Ok(Response::new(proto::StatChunkResponse {})),
49            Ok(false) => Err(Status::not_found(format!("chunk {digest} not found"))),
50            Err(e) => {
51                warn!(err = %e, "failed to check for chunk");
52                Err(Status::internal(e.to_string()))
53            }
54        }
55    }
56
57    #[instrument(skip_all, fields(chunk.digest=tracing::field::Empty))]
58    async fn get_chunk(
59        &self,
60        request: Request<proto::GetChunkRequest>,
61    ) -> Result<Response<Self::GetChunkStream>, Status> {
62        let digest: B3Digest = request
63            .into_inner()
64            .digest
65            .try_into()
66            .map_err(|_| Status::invalid_argument("invalid digest length"))?;
67
68        let span = Span::current();
69        span.record("chunk.digest", digest.to_string());
70
71        match self.chunk_store.get(&digest).await {
72            Ok(Some(chunk)) => {
73                let chunk_bytes: bytes::Bytes = chunk.into();
74                let chunk_len = chunk_bytes.len();
75                let stream =
76                    futures::stream::iter((0..chunk_len).step_by(GRPC_CHUNK_SIZE).map(move |i| {
77                        Ok(proto::GetChunkResponse {
78                            data: chunk_bytes.slice(i..(i + GRPC_CHUNK_SIZE).min(chunk_len)),
79                        })
80                    }));
81                Ok(Response::new(Box::pin(stream)))
82            }
83            Ok(None) => Err(Status::not_found(format!("chunk {digest} not found"))),
84            Err(e) => {
85                warn!(err = %e, "failed to get chunk");
86                Err(Status::internal(e.to_string()))
87            }
88        }
89    }
90
91    #[instrument(skip_all)]
92    async fn put_chunk(
93        &self,
94        request: Request<Streaming<proto::PutChunkRequest>>,
95    ) -> Result<Response<proto::PutChunkResponse>, Status> {
96        let mut stream = request.into_inner();
97
98        let mut chunk_builder = Chunk::builder();
99        while let Some(proto::PutChunkRequest { data }) = stream.message().await? {
100            chunk_builder.append_bytes(data);
101        }
102        let chunk = chunk_builder.build();
103
104        let digest = self.chunk_store.put(chunk).await.map_err(|err| {
105            warn!(%err, "failed to put chunk");
106            Status::internal(err.to_string())
107        })?;
108
109        Ok(Response::new(proto::PutChunkResponse {
110            digest: digest.into(),
111        }))
112    }
113}