mito2/series_index/samples/
bulk.rs1use std::sync::Arc;
18
19use bytes::Bytes;
20use object_store::ObjectStore;
21use snafu::ResultExt;
22use store_api::storage::{FileId, RegionId};
23
24use crate::access_layer::OperationType;
25use crate::cache::file_cache::{FileType, IndexKey};
26use crate::cache::write_cache::{UploadOptions, WriteCache};
27use crate::error::{OpenDalSnafu, Result};
28use crate::series_index::samples::metadata::{Block, Column};
29use crate::series_index::samples::{MemoryArtifact, ValueColumn};
30use crate::sst::location::{narthex_manifest_path, narthex_part_path};
31use crate::sst::{DEFAULT_WRITE_BUFFER_SIZE, DEFAULT_WRITE_CONCURRENCY};
32
33#[derive(Debug, Clone)]
34pub(crate) struct NarthexBulkPart(pub(super) Arc<MemoryArtifact>, usize);
35
36impl NarthexBulkPart {
37 pub(super) fn new(artifact: MemoryArtifact) -> Self {
38 let retained_bytes = size_of::<MemoryArtifact>()
39 + artifact.manifest.len()
40 + artifact.keys.len()
41 + artifact.parts.capacity() * size_of::<Bytes>()
42 + artifact.parts.iter().map(Bytes::len).sum::<usize>()
43 + artifact.blocks.capacity() * size_of::<(usize, Block)>()
44 + artifact
45 .blocks
46 .iter()
47 .map(|(_, b)| b.columns.capacity() * size_of::<Column>())
48 .sum::<usize>()
49 + artifact.fields.capacity() * size_of::<ValueColumn>()
50 + artifact
51 .fields
52 .iter()
53 .map(|f| f.name.capacity() + f.data_type.capacity())
54 .sum::<usize>();
55 Self(Arc::new(artifact), retained_bytes)
56 }
57
58 pub(crate) async fn upload(
59 &self,
60 store: &ObjectStore,
61 prefix: &str,
62 region_id: RegionId,
63 cache: Option<&WriteCache>,
64 ) -> Result<()> {
65 let parts = self.parts().iter().enumerate().map(|(i, data)| {
66 (
67 narthex_part_path(prefix, i, "samples"),
68 Some(i as u32),
69 data,
70 )
71 });
72 for (path, part, bytes) in parts.chain(std::iter::once((
73 narthex_manifest_path(prefix),
74 None,
75 self.manifest(),
76 ))) {
77 if let Some(cache) = cache {
78 let key = IndexKey::new(region_id, self.file_id(), FileType::Samples(part));
79 let files = cache.file_cache();
80 write_object(
81 &files.local_store(),
82 &files.cache_file_path(key),
83 bytes.clone(),
84 )
85 .await
86 .context(OpenDalSnafu)?;
87 cache
88 .upload(
89 key,
90 &path,
91 store,
92 UploadOptions {
93 op_type: OperationType::Flush,
94 write_buffer_size: DEFAULT_WRITE_BUFFER_SIZE,
95 },
96 )
97 .await?;
98 } else if let Err(error) = write_object(store, &path, bytes.clone()).await {
99 if let Err(err) = store.delete_with(prefix).recursive(true).await {
100 common_telemetry::warn!(err; "Cleaning failed Narthex upload {prefix}");
101 }
102 return Err(error).context(OpenDalSnafu);
103 }
104 }
105 Ok(())
106 }
107
108 pub(crate) fn series_count(&self) -> u64 {
109 self.0
110 .blocks
111 .iter()
112 .filter(|(_, block)| block.ordinal == 0)
113 .count() as u64
114 }
115
116 pub(crate) fn file_id(&self) -> FileId {
117 self.0.file_id
118 }
119 pub(crate) fn manifest(&self) -> &Bytes {
120 &self.0.manifest
121 }
122 pub(crate) fn parts(&self) -> &[Bytes] {
123 &self.0.parts
124 }
125 pub(crate) fn size_bytes(&self) -> usize {
126 self.1
127 }
128
129 pub(super) fn key(&self, block: &Block) -> Bytes {
130 let start = block.key.offset as usize;
131 self.0.keys.slice(start..start + block.key.length as usize)
132 }
133
134 pub(crate) fn primary_keys(&self) -> crate::memtable::BoxedPrimaryKeyIterator {
135 let part = self.clone();
136 Box::new((0..part.0.blocks.len()).map(move |index| {
137 let (_, block) = &part.0.blocks[index];
138 part.key(block)
139 }))
140 }
141}
142
143async fn write_object(store: &ObjectStore, path: &str, bytes: Bytes) -> object_store::Result<()> {
144 let mut writer = store
145 .writer_with(path)
146 .chunk(DEFAULT_WRITE_BUFFER_SIZE.as_bytes() as usize)
147 .concurrent(DEFAULT_WRITE_CONCURRENCY)
148 .await?;
149 let result = match writer.write(bytes).await {
150 Ok(()) => writer.close().await.map(|_| ()),
151 Err(error) => Err(error),
152 };
153 if result.is_err()
154 && let Err(err) = writer.abort().await
155 {
156 common_telemetry::warn!(err; "Aborting failed Narthex upload {path}");
157 }
158 result
159}