Repository navigation
Expand file tree
/
Copy pathnamespace_manifest.rs
More file actions
299 lines (267 loc) · 9.82 KB
/
Copy pathnamespace_manifest.rs
File metadata and controls
299 lines (267 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors
use std::sync::Arc;
use async_trait::async_trait;
use lance_core::Result;
use lance_namespace::LanceNamespace;
use lance_namespace::models::{
CreateTableVersionRequest, DescribeTableVersionRequest, ListTableVersionsRequest,
};
use lance_table::io::commit::external_manifest::ExternalManifestStore;
use lance_table::io::commit::{ManifestLocation, ManifestNamingScheme};
use object_store::ObjectStore as OSObjectStore;
use object_store::path::Path;
use lance_namespace::error::NamespaceError;
use crate::dataset::branch_location::BranchLocation;
/// Whether `e` says the requested chain (table or branch) does not exist, as
/// opposed to a failure talking to the namespace.
fn is_chain_not_found(e: &lance_core::Error) -> bool {
if let lance_core::Error::Namespace { source, .. } = e
&& let Some(ns_err) = source.downcast_ref::<NamespaceError>()
{
return matches!(
ns_err,
NamespaceError::TableNotFound { .. } | NamespaceError::TableBranchNotFound { .. }
);
}
false
}
#[derive(Debug)]
pub struct LanceNamespaceExternalManifestStore {
namespace_client: Arc<dyn LanceNamespace>,
table_id: Vec<String>,
/// Object-store path of the table root (the main branch). The base path the
/// trait methods receive is resolved against this to derive which branch a
/// request targets, so a single store serves every branch of the table.
table_root: Path,
}
impl LanceNamespaceExternalManifestStore {
pub fn new(
namespace_client: Arc<dyn LanceNamespace>,
table_id: Vec<String>,
table_root: Path,
) -> Self {
Self {
namespace_client,
table_id,
table_root,
}
}
/// Build a store for the table rooted at `table_uri`, resolving the root
/// path from the uri without initializing an object store.
pub fn for_table_uri(
namespace_client: Arc<dyn LanceNamespace>,
table_id: Vec<String>,
table_uri: &str,
) -> Result<Self> {
let table_root = lance_io::object_store::ObjectStore::extract_path_from_uri(
Arc::new(lance_io::object_store::ObjectStoreRegistry::default()),
table_uri,
)?;
Ok(Self::new(namespace_client, table_id, table_root))
}
/// Derive the branch targeted by `base` (the table root for main, or a
/// branch chain produced by `BranchLocation::find_branch`). The branch
/// path layout is owned by [`BranchLocation`]; this store never parses or
/// constructs it directly.
fn branch_for_base(&self, base: &str) -> Result<Option<String>> {
BranchLocation::branch_of(self.table_root.as_ref(), base)
}
}
#[async_trait]
impl ExternalManifestStore for LanceNamespaceExternalManifestStore {
async fn get(&self, base_uri: &str, version: u64) -> Result<String> {
let request = DescribeTableVersionRequest {
id: Some(self.table_id.clone()),
version: Some(version as i64),
branch: self.branch_for_base(base_uri)?,
..Default::default()
};
let response = self
.namespace_client
.describe_table_version(request)
.await?;
// Namespace returns full path (relative to object store root)
Ok(response.version.manifest_path)
}
async fn get_latest_version(&self, base_uri: &str) -> Result<Option<(u64, String)>> {
let request = ListTableVersionsRequest {
id: Some(self.table_id.clone()),
descending: Some(true),
limit: Some(1),
branch: self.branch_for_base(base_uri)?,
..Default::default()
};
let response = match self.namespace_client.list_table_versions(request).await {
Ok(response) => response,
// A chain that does not exist yet (e.g. probing a branch location
// before the branch is created) has no latest version; the
// ExternalManifestStore contract reports that as None, not an
// error, so existence checks can treat it as a missing dataset.
Err(e) if is_chain_not_found(&e) => return Ok(None),
Err(e) => return Err(e),
};
if response.versions.is_empty() {
return Ok(None);
}
let version = &response.versions[0];
// Namespace returns full path (relative to object store root)
Ok(Some((
version.version as u64,
version.manifest_path.clone(),
)))
}
/// Put the manifest to the namespace store.
async fn put(
&self,
base_path: &Path,
version: u64,
staging_path: &Path,
size: u64,
e_tag: Option<String>,
_object_store: &dyn OSObjectStore,
naming_scheme: ManifestNamingScheme,
) -> Result<ManifestLocation> {
// create_table_version reads staging manifest and writes to final location
let naming_scheme_str = match naming_scheme {
ManifestNamingScheme::V1 => "V1",
ManifestNamingScheme::V2 => "V2",
};
let request = CreateTableVersionRequest {
id: Some(self.table_id.clone()),
version: version as i64,
manifest_path: staging_path.to_string(),
manifest_size: Some(size as i64),
e_tag: e_tag.clone(),
naming_scheme: Some(naming_scheme_str.to_string()),
branch: self.branch_for_base(base_path.as_ref())?,
..Default::default()
};
let response = self.namespace_client.create_table_version(request).await?;
// Get version info from response
let version_info = response.version.ok_or_else(|| {
lance_core::Error::internal(
"create_table_version response missing version info".to_string(),
)
})?;
Ok(ManifestLocation {
version: version_info.version as u64,
path: Path::parse(&version_info.manifest_path).map_err(|e| {
lance_core::Error::invalid_input(format!(
"Invalid manifest path '{}': {}",
version_info.manifest_path, e
))
})?,
size: version_info.manifest_size.map(|s| s as u64),
naming_scheme,
e_tag: version_info.e_tag,
identity: None,
})
}
async fn put_if_not_exists(
&self,
_base_uri: &str,
_version: u64,
_path: &str,
_size: u64,
_e_tag: Option<String>,
) -> Result<()> {
Err(lance_core::Error::not_supported_source(
"put_if_not_exists is not supported for namespace-backed stores".into(),
))
}
async fn put_if_exists(
&self,
_base_uri: &str,
_version: u64,
_path: &str,
_size: u64,
_e_tag: Option<String>,
) -> Result<()> {
Err(lance_core::Error::not_supported_source(
"put_if_exists is not supported for namespace-backed stores".into(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use lance_namespace::models::ListTableVersionsResponse;
/// A namespace whose list_table_versions always fails with the configured
/// error, to pin how get_latest_version classifies failures.
#[derive(Debug)]
struct FailingNamespace {
error: fn() -> lance_core::Error,
}
#[async_trait]
impl LanceNamespace for FailingNamespace {
fn namespace_id(&self) -> String {
"failing".to_string()
}
async fn list_table_versions(
&self,
_request: ListTableVersionsRequest,
) -> Result<ListTableVersionsResponse> {
Err((self.error)())
}
}
fn store_with(error: fn() -> lance_core::Error) -> LanceNamespaceExternalManifestStore {
LanceNamespaceExternalManifestStore::new(
Arc::new(FailingNamespace { error }),
vec!["t".to_string()],
Path::parse("data/t.lance").unwrap(),
)
}
/// A chain that does not exist (missing table or branch) has no latest
/// version; everything else is a real failure and must propagate so an
/// outage is never mistaken for an absent dataset.
#[tokio::test]
async fn test_get_latest_version_error_classification() {
use lance_namespace::error::NamespaceError;
let absent = [
store_with(|| {
NamespaceError::TableNotFound {
message: "missing table".to_string(),
}
.into()
}),
store_with(|| {
NamespaceError::TableBranchNotFound {
message: "missing branch".to_string(),
}
.into()
}),
];
for store in absent {
let latest = store.get_latest_version("data/t.lance/tree/dev").await;
assert!(
matches!(latest, Ok(None)),
"a missing chain must read as no latest version, got: {:?}",
latest
);
}
let failures = [
store_with(|| {
NamespaceError::Internal {
message: "server error".to_string(),
}
.into()
}),
store_with(|| {
NamespaceError::Throttling {
message: "slow down".to_string(),
}
.into()
}),
store_with(|| lance_core::Error::io("connection reset".to_string())),
];
for store in failures {
let latest = store.get_latest_version("data/t.lance/tree/dev").await;
assert!(
latest.is_err(),
"a real failure must propagate, got: {:?}",
latest
);
}
}
}