harmos_operations/storage/
follow_production.rs1use super::IO_POLICY;
24use harmos_domain::MemorySize;
25use harmos_domain::model::storage::codec::{Nested, Unsigned, product};
26use harmos_domain::model::storage::files::{FileBatch, FileProgress, StagedFile};
27use harmos_domain::operations::ReplaySafety;
28#[cfg(feature = "runtime")]
29use harmos_sdk::operations::access::GrantAccess;
30#[cfg(feature = "runtime")]
31use harmos_storage::{ErasedCatalog, StorageManager};
32
33product!(FollowProduction {
38 file: Nested<StagedFile> => 1,
39 offset: Unsigned => 2,
40});
41
42#[harmos::operation(
47 harmos_path = crate,
48 resources = [StorageManager<ErasedCatalog>, GrantAccess],
49 codec = manual,
50 id = "storage.follow_production",
51 response = FileProgress,
52 policy = IO_POLICY,
53 output = FileBatch,
54 visibility = Shared,
55 replay = ReplaySafety::Unknown,
56 grpc(package = "harmos.storage.v1", service = "Storage", method = "FollowProduction"),
57 grant = Self::grant_for,
58)]
59#[derive(Clone, Debug, PartialEq, Eq)]
60pub struct FollowProduction {
61 pub file: StagedFile,
62 pub offset: u64,
63}
64
65impl FollowProduction {
72 fn grant_for(request: &FollowProduction) -> Option<harmos_domain::plugin::Grant> {
73 Some(harmos_domain::plugin::Grant::of(<Self as harmos_domain::OperationDeclaration>::ID, request.file.id.as_str().to_owned()))
74 }
75}
76
77impl MemorySize for FollowProduction {
78 fn memory_size(&self) -> usize {
79 std::mem::size_of::<Self>()
80 .saturating_add(self.file.memory_size().saturating_sub(std::mem::size_of_val(&self.file)))
81 .saturating_add(self.offset.memory_size().saturating_sub(std::mem::size_of_val(&self.offset)))
82 }
83}
84
85#[cfg(feature = "runtime")]
86mod execution {
87 use super::*;
88
89 use crate::storage::IO_TIMEOUT;
97 use crate::{Error, Result};
98 use harmos_sdk::operations::{Operation, OperationDeclaration};
99 use harmos_storage::FileProgress;
100
101 impl Operation for super::FollowProduction {
102 type Error = Error;
103
104 #[tracing::instrument(target = "harmos::storage", name = "storage.operation.follow_production", level = "trace", skip_all)]
105 async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<harmos_sdk::operations::Response<FileProgress>> {
106 let storage = context.resource::<harmos_storage::StorageManager<harmos_storage::ErasedCatalog>>();
107 let grants = context.resource::<harmos_sdk::operations::access::GrantAccess>();
108 let output = context.streams();
109
110 let host = grants;
111 let ctx = &host.0;
112 let grant = harmos_domain::plugin::Grant::of(FollowProduction::ID, self.file.id.as_str().to_owned());
113 ctx.admit(&grant).map_err(Error::Access)?;
114 let mut follower = storage.follow_production(&self.file, self.offset).await?;
115 let batch_bytes = storage.settings().response.batch_bytes;
116 while let Some(batch) = follower.next_batch(batch_bytes).await? {
117 ctx.admit(&grant).map_err(Error::Access)?;
118 let response = harmos_sdk::operations::Response::from_owner(batch.owner());
119 tokio::time::timeout(IO_TIMEOUT, output.send_owned(response)).await.map_err(|_| crate::storage::output_timeout())??;
120 }
121 Ok(follower.progress().into())
122 }
123
124 fn classify(error: &Error) -> harmos_sdk::operations::FailureKind {
125 crate::storage::classify(error)
126 }
127 }
128}
129
130