Coverage Report

Created: 2025-04-19 16:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/build/source/nativelink-service/src/fetch_server.rs
Line
Count
Source
1
// Copyright 2025 The NativeLink Authors. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
use std::convert::Into;
16
17
use nativelink_config::cas_server::FetchConfig;
18
use nativelink_error::{Code, Error, ResultExt, make_err};
19
use nativelink_proto::build::bazel::remote::asset::v1::fetch_server::{
20
    Fetch, FetchServer as Server,
21
};
22
use nativelink_proto::build::bazel::remote::asset::v1::{
23
    FetchBlobRequest, FetchBlobResponse, FetchDirectoryRequest, FetchDirectoryResponse,
24
};
25
use nativelink_store::store_manager::StoreManager;
26
use nativelink_util::digest_hasher::{default_digest_hasher_func, make_ctx_for_hash_func};
27
use nativelink_util::origin_event::OriginEventContext;
28
use tonic::{Request, Response, Status};
29
use tracing::{Level, error_span, instrument};
30
31
#[derive(Debug, Clone, Copy)]
32
pub struct FetchServer {}
33
34
impl FetchServer {
35
1
    pub const fn new(_config: &FetchConfig, _store_manager: &StoreManager) -> Result<Self, Error> {
36
1
        Ok(FetchServer {})
37
1
    }
38
39
0
    pub fn into_service(self) -> Server<FetchServer> {
40
0
        Server::new(self)
41
0
    }
42
43
1
    async fn inner_fetch_blob(
44
1
        &self,
45
1
        request: FetchBlobRequest,
46
1
    ) -> Result<Response<FetchBlobResponse>, Error> {
47
1
        Ok(Response::new(FetchBlobResponse {
48
1
            status: Some(make_err!(Code::NotFound, "No item found").into()),
49
1
            uri: request.uris.first().cloned().unwrap_or(String::new()),
50
1
            qualifiers: vec![],
51
1
            expires_at: None,
52
1
            blob_digest: None,
53
1
            digest_function: default_digest_hasher_func().proto_digest_func().into(),
54
1
        }))
55
1
    }
56
}
57
58
#[tonic::async_trait]
59
impl Fetch for FetchServer {
60
    #[allow(clippy::blocks_in_conditions)]
61
    #[instrument(
62
        err,
63
        ret(level = Level::INFO),
64
        level = Level::ERROR,
65
        skip_all,
66
        fields(request = ?grpc_request.get_ref())
67
    )]
68
    async fn fetch_blob(
69
        &self,
70
        grpc_request: Request<FetchBlobRequest>,
71
2
    ) -> Result<Response<FetchBlobResponse>, Status> {
72
1
        let request = grpc_request.into_inner();
73
1
        let ctx = OriginEventContext::new(|| 
&request0
).await;
74
1
        let resp: Result<Response<FetchBlobResponse>, Status> =
75
1
            make_ctx_for_hash_func(request.digest_function)
76
1
                .err_tip(|| 
"In FetchServer::fetch_blob"0
)
?0
77
1
                .wrap_async(
78
1
                    error_span!("fetch_server_fetch_blob"),
79
1
                    self.inner_fetch_blob(request),
80
1
                )
81
1
                .await
82
1
                .err_tip(|| 
"Failed on fetch_blob() command"0
)
83
1
                .map_err(Into::into);
84
1
        ctx.emit(|| 
&resp0
).await;
85
1
        resp
86
2
    }
87
88
    #[allow(clippy::blocks_in_conditions)]
89
    #[instrument(
90
        err,
91
        ret(level = Level::INFO),
92
        level = Level::ERROR,
93
        skip_all,
94
        fields(request = ?_grpc_request.get_ref())
95
    )]
96
    async fn fetch_directory(
97
        &self,
98
        _grpc_request: Request<FetchDirectoryRequest>,
99
0
    ) -> Result<Response<FetchDirectoryResponse>, Status> {
100
0
        todo!()
101
0
    }
102
}