|
| 1 | +// Copyright 2023 The Turbo Cache 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::pin::Pin; |
| 16 | +use std::sync::Arc; |
| 17 | + |
| 18 | +use ac_utils::get_and_decode_digest; |
| 19 | +use action_messages::ActionResult; |
| 20 | +use async_trait::async_trait; |
| 21 | +use futures::stream::{self, StreamExt, TryStreamExt}; |
| 22 | +use proto::build::bazel::remote::execution::v2::{ActionResult as ProtoActionResult, Tree}; |
| 23 | + |
| 24 | +use buf_channel::{DropCloserReadHalf, DropCloserWriteHalf}; |
| 25 | +use common::DigestInfo; |
| 26 | +use error::{Error, ResultExt}; |
| 27 | +use traits::{StoreTrait, UploadSizeInfo}; |
| 28 | + |
| 29 | +pub struct CompletenessCheckingStore { |
| 30 | + cas_store: Arc<dyn StoreTrait>, |
| 31 | +} |
| 32 | + |
| 33 | +impl CompletenessCheckingStore { |
| 34 | + pub fn new(_ac_store: Arc<dyn StoreTrait>, cas_store: Arc<dyn StoreTrait>) -> Self { |
| 35 | + CompletenessCheckingStore { cas_store } |
| 36 | + } |
| 37 | + |
| 38 | + fn pin_cas(&self) -> Pin<&dyn StoreTrait> { |
| 39 | + Pin::new(self.cas_store.as_ref()) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[async_trait] |
| 44 | +impl StoreTrait for CompletenessCheckingStore { |
| 45 | + async fn has_with_results( |
| 46 | + self: Pin<&Self>, |
| 47 | + digests: &[DigestInfo], |
| 48 | + _results: &mut [Option<usize>], |
| 49 | + ) -> Result<(), Error> { |
| 50 | + // The proto promises that all results will exist in the CAS when |
| 51 | + // requested using the associated actions. However we currently allow |
| 52 | + // stores to prune themselves which violates this condition, because the |
| 53 | + // action cache and CAS are different. Therefore we need this completeness checking |
| 54 | + // store to check that all results exist in the CAS before we allow the the action result |
| 55 | + // to be returned to the client. |
| 56 | + // * Take the root digest which is the serialzied action proto hash |
| 57 | + // * Deserialize the action proto |
| 58 | + // * Check files in the root tree exist in the CAS but there's directories that needs |
| 59 | + // to be traversed as the directories can have directories and files in them and esure all |
| 60 | + // of them exist in the CAS. |
| 61 | + |
| 62 | + for digest in digests { |
| 63 | + let action_result: ActionResult = get_and_decode_digest::<ProtoActionResult>(self.pin_cas(), digest) |
| 64 | + .await? |
| 65 | + .try_into() |
| 66 | + .err_tip(|| "Action result could not be converted in completeness checking store has")?; |
| 67 | + |
| 68 | + // Something that takes a stream of iterators and flattens it into a stream of items |
| 69 | + // 'temp' is a stream of digests obtained from the output folders of the action result |
| 70 | + let _temp = |
| 71 | + stream::iter(action_result.output_folders) |
| 72 | + .then(|directory| async move { |
| 73 | + get_and_decode_digest::<Tree>(self.pin_cas(), &directory.tree_digest).await |
| 74 | + }) |
| 75 | + .map_ok(|tree| async { |
| 76 | + let trees = |
| 77 | + tree.children |
| 78 | + .into_iter() |
| 79 | + .chain(tree.root.into_iter()) |
| 80 | + .flat_map(|child_directory| { |
| 81 | + child_directory.files.into_iter().filter_map(|file| file.digest) |
| 82 | + }); |
| 83 | + |
| 84 | + let item_stream = stream::iter(trees); |
| 85 | + item_stream |
| 86 | + .map(|file_digest| async move { |
| 87 | + let digest_info = |
| 88 | + DigestInfo::try_new(&file_digest.hash, file_digest.size_bytes as usize)?; |
| 89 | + self.pin_cas().has(digest_info).await |
| 90 | + }) |
| 91 | + .buffer_unordered(10) |
| 92 | + .try_collect::<Vec<_>>() |
| 93 | + .await |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | + |
| 100 | + async fn update( |
| 101 | + self: Pin<&Self>, |
| 102 | + digest: DigestInfo, |
| 103 | + reader: DropCloserReadHalf, |
| 104 | + size_info: UploadSizeInfo, |
| 105 | + ) -> Result<(), Error> { |
| 106 | + self.pin_cas().update(digest, reader, size_info).await |
| 107 | + } |
| 108 | + |
| 109 | + async fn get_part_ref( |
| 110 | + self: Pin<&Self>, |
| 111 | + digest: DigestInfo, |
| 112 | + writer: &mut DropCloserWriteHalf, |
| 113 | + offset: usize, |
| 114 | + length: Option<usize>, |
| 115 | + ) -> Result<(), Error> { |
| 116 | + self.pin_cas().get_part_ref(digest, writer, offset, length).await |
| 117 | + } |
| 118 | + |
| 119 | + fn as_any(self: Arc<Self>) -> Box<dyn std::any::Any + Send> { |
| 120 | + Box::new(self) |
| 121 | + } |
| 122 | +} |
0 commit comments