-
Notifications
You must be signed in to change notification settings - Fork 741
feat(core): service add DBFS API 2.0 support #3334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a2ec651
feat: service add dbfs api 2.0 support
morristai 8ebe671
feat: re-use Http client in crate and formatting
morristai 2e26409
chore: format modify for clippy check
morristai da1d8e0
feat: create dedicated core struct for backend
morristai a06ad4c
feat: implement DBFS reader
morristai 2816380
chore: drop scope change for HttpClient
morristai a752033
feat: fix PR review #3334
morristai 86d8f5e
Merge branch 'main' into feat/service_add_dbfs_api_2.0
morristai cfd5d11
feat: fix PR review #3334
morristai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,315 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::fmt::Debug; | ||
| use std::fmt::Formatter; | ||
| use std::sync::Arc; | ||
|
|
||
| use async_trait::async_trait; | ||
| use http::StatusCode; | ||
| use log::debug; | ||
| use serde::Deserialize; | ||
|
|
||
| use crate::raw::*; | ||
| use crate::*; | ||
|
|
||
| use super::core::DbfsCore; | ||
| use super::error::parse_error; | ||
| use super::pager::DbfsPager; | ||
| use super::reader::DbfsReader; | ||
| use super::writer::DbfsWriter; | ||
|
|
||
| /// [Dbfs](https://docs.databricks.com/api/azure/workspace/dbfs)'s REST API support. | ||
| #[doc = include_str!("docs.md")] | ||
| #[derive(Default, Clone)] | ||
| pub struct DbfsBuilder { | ||
| root: Option<String>, | ||
| endpoint: Option<String>, | ||
| token: Option<String>, | ||
| } | ||
|
|
||
| impl Debug for DbfsBuilder { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| let mut ds = f.debug_struct("Builder"); | ||
|
|
||
| ds.field("root", &self.root); | ||
| ds.field("endpoint", &self.endpoint); | ||
|
|
||
| if self.token.is_some() { | ||
| ds.field("token", &"<redacted>"); | ||
| } | ||
|
|
||
| ds.finish() | ||
| } | ||
| } | ||
|
|
||
| impl DbfsBuilder { | ||
| /// Set root of this backend. | ||
| /// | ||
| /// All operations will happen under this root. | ||
| pub fn root(&mut self, root: &str) -> &mut Self { | ||
| if !root.is_empty() { | ||
| self.root = Some(root.to_string()) | ||
| } | ||
|
|
||
| self | ||
| } | ||
|
|
||
| /// Set endpoint of this backend. | ||
| /// | ||
| /// Endpoint must be full uri, e.g. | ||
| /// | ||
| /// - Azure: `https://adb-1234567890123456.78.azuredatabricks.net` | ||
| /// - Aws: `https://dbc-123a5678-90bc.cloud.databricks.com` | ||
| pub fn endpoint(&mut self, endpoint: &str) -> &mut Self { | ||
| self.endpoint = if endpoint.is_empty() { | ||
| None | ||
| } else { | ||
| Some(endpoint.trim_end_matches('/').to_string()) | ||
| }; | ||
| self | ||
| } | ||
|
|
||
| /// Set the token of this backend. | ||
| pub fn token(&mut self, token: &str) -> &mut Self { | ||
| if !token.is_empty() { | ||
| self.token = Some(token.to_string()); | ||
| } | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Builder for DbfsBuilder { | ||
| const SCHEME: Scheme = Scheme::Dbfs; | ||
| type Accessor = DbfsBackend; | ||
|
|
||
| fn from_map(map: HashMap<String, String>) -> Self { | ||
| let mut builder = DbfsBuilder::default(); | ||
|
|
||
| map.get("endpoint").map(|v| builder.endpoint(v)); | ||
| map.get("token").map(|v| builder.token(v)); | ||
|
|
||
| builder | ||
| } | ||
|
|
||
| /// Build a DbfsBackend. | ||
| fn build(&mut self) -> Result<Self::Accessor> { | ||
| debug!("backend build started: {:?}", &self); | ||
|
|
||
| let root = normalize_root(&self.root.take().unwrap_or_default()); | ||
| debug!("backend use root {}", root); | ||
|
|
||
| let endpoint = match &self.endpoint { | ||
| Some(endpoint) => Ok(endpoint.clone()), | ||
| None => Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty") | ||
| .with_operation("Builder::build") | ||
| .with_context("service", Scheme::Dbfs)), | ||
| }?; | ||
| debug!("backend use endpoint: {}", &endpoint); | ||
|
|
||
| let token = match self.token.take() { | ||
| Some(token) => token, | ||
| None => { | ||
| return Err(Error::new( | ||
| ErrorKind::ConfigInvalid, | ||
| "missing token for Dbfs", | ||
| )); | ||
| } | ||
| }; | ||
|
|
||
| let client = HttpClient::new()?; | ||
|
|
||
| debug!("backend build finished: {:?}", &self); | ||
| Ok(DbfsBackend { | ||
| core: Arc::new(DbfsCore { | ||
| root, | ||
| endpoint: endpoint.to_string(), | ||
| token, | ||
| client, | ||
| }), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Backend for DBFS service | ||
| #[derive(Debug, Clone)] | ||
| pub struct DbfsBackend { | ||
| core: Arc<DbfsCore>, | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl Accessor for DbfsBackend { | ||
| type Reader = DbfsReader; | ||
| type BlockingReader = (); | ||
| type Writer = oio::OneShotWriter<DbfsWriter>; | ||
| type BlockingWriter = (); | ||
| type Pager = DbfsPager; | ||
| type BlockingPager = (); | ||
|
|
||
| fn info(&self) -> AccessorInfo { | ||
| let mut am = AccessorInfo::default(); | ||
| am.set_scheme(Scheme::Dbfs) | ||
| .set_root(&self.core.root) | ||
| .set_native_capability(Capability { | ||
| stat: true, | ||
|
|
||
| read: true, | ||
| read_can_next: true, | ||
| read_with_range: true, | ||
|
|
||
| write: true, | ||
| create_dir: true, | ||
| delete: true, | ||
| rename: true, | ||
|
|
||
| list: true, | ||
| list_with_delimiter_slash: true, | ||
|
|
||
| ..Default::default() | ||
| }); | ||
| am | ||
| } | ||
|
|
||
| async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> { | ||
| let resp = self.core.dbfs_create_dir(path).await?; | ||
|
|
||
| let status = resp.status(); | ||
|
|
||
| match status { | ||
| StatusCode::CREATED | StatusCode::OK => { | ||
| resp.into_body().consume().await?; | ||
| Ok(RpCreateDir::default()) | ||
| } | ||
| _ => Err(parse_error(resp).await?), | ||
| } | ||
| } | ||
|
|
||
| async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> { | ||
| let mut meta = Metadata::new(EntryMode::FILE); | ||
|
|
||
| if let Some(length) = args.range().size() { | ||
| meta.set_content_length(length); | ||
| } else { | ||
| let stat_resp = self.core.dbfs_get_status(path).await?; | ||
| meta = parse_into_metadata(path, stat_resp.headers())?; | ||
| let decoded_response = | ||
| serde_json::from_slice::<DbfsStatus>(&stat_resp.into_body().bytes().await?) | ||
| .map_err(new_json_deserialize_error)?; | ||
| meta.set_last_modified(parse_datetime_from_from_timestamp_millis( | ||
| decoded_response.modification_time, | ||
| )?); | ||
| meta.set_mode(if decoded_response.is_dir { | ||
| EntryMode::DIR | ||
| } else { | ||
| EntryMode::FILE | ||
| }); | ||
| if !decoded_response.is_dir { | ||
| meta.set_content_length(decoded_response.file_size as u64); | ||
| } | ||
| } | ||
|
|
||
| let op = DbfsReader::new(self.core.clone(), args, path.to_string()); | ||
|
|
||
| Ok((RpRead::with_metadata(meta), op)) | ||
| } | ||
|
|
||
| async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> { | ||
| Ok(( | ||
| RpWrite::default(), | ||
| oio::OneShotWriter::new(DbfsWriter::new(self.core.clone(), args, path.to_string())), | ||
| )) | ||
| } | ||
|
|
||
| async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> { | ||
| self.core.dbfs_ensure_parent_path(to).await?; | ||
|
|
||
| let resp = self.core.dbfs_rename(from, to).await?; | ||
|
|
||
| let status = resp.status(); | ||
|
|
||
| match status { | ||
| StatusCode::OK => { | ||
| resp.into_body().consume().await?; | ||
| Ok(RpRename::default()) | ||
| } | ||
| _ => Err(parse_error(resp).await?), | ||
| } | ||
| } | ||
|
|
||
| async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> { | ||
| // Stat root always returns a DIR. | ||
| if path == "/" { | ||
| return Ok(RpStat::new(Metadata::new(EntryMode::DIR))); | ||
| } | ||
|
|
||
| let resp = self.core.dbfs_get_status(path).await?; | ||
|
|
||
| let status = resp.status(); | ||
|
|
||
| match status { | ||
| StatusCode::OK => { | ||
| let mut meta = parse_into_metadata(path, resp.headers())?; | ||
| let bs = resp.into_body().bytes().await?; | ||
| let decoded_response = serde_json::from_slice::<DbfsStatus>(&bs) | ||
| .map_err(new_json_deserialize_error)?; | ||
| meta.set_last_modified(parse_datetime_from_from_timestamp_millis( | ||
| decoded_response.modification_time, | ||
| )?); | ||
| match decoded_response.is_dir { | ||
| true => meta.set_mode(EntryMode::DIR), | ||
| false => { | ||
| meta.set_mode(EntryMode::FILE); | ||
| meta.set_content_length(decoded_response.file_size as u64) | ||
| } | ||
| }; | ||
| Ok(RpStat::new(meta)) | ||
| } | ||
| StatusCode::NOT_FOUND if path.ends_with('/') => { | ||
| Ok(RpStat::new(Metadata::new(EntryMode::DIR))) | ||
| } | ||
| _ => Err(parse_error(resp).await?), | ||
| } | ||
| } | ||
|
|
||
| /// NOTE: Server will return 200 even if the path doesn't exist. | ||
| async fn delete(&self, path: &str, _: OpDelete) -> Result<RpDelete> { | ||
| let resp = self.core.dbfs_delete(path).await?; | ||
|
|
||
| let status = resp.status(); | ||
|
|
||
| match status { | ||
| StatusCode::OK => Ok(RpDelete::default()), | ||
| _ => Err(parse_error(resp).await?), | ||
| } | ||
| } | ||
|
|
||
| async fn list(&self, path: &str, _args: OpList) -> Result<(RpList, Self::Pager)> { | ||
| let op = DbfsPager::new(self.core.clone(), path.to_string()); | ||
|
|
||
| Ok((RpList::default(), op)) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct DbfsStatus { | ||
| // Not used fields. | ||
| // path: String, | ||
| is_dir: bool, | ||
| file_size: i64, | ||
| modification_time: i64, | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.