-
Notifications
You must be signed in to change notification settings - Fork 790
feat(services): add hdfs native layout #3933
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 4 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
3a2e6cd
intial commit for native-hdfs service
shbhmrzd 237c1fd
Merge conflict resolved
shbhmrzd b21ea23
revert async-tls change
shbhmrzd 6f1d3ef
review comments on layout
shbhmrzd 3f7eaca
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd ab2ab3c
remove doc
shbhmrzd 509e1a8
fix import issues
shbhmrzd ceaa7a1
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 8e1b8ab
fix import errors
shbhmrzd ddab8c3
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 35cac1e
change service name
shbhmrzd 9d33fe4
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 81fbe2f
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 8afe6ba
parse hdfs-native error into opendal error
shbhmrzd 772a186
cargo fmt
shbhmrzd 2328b56
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 9e34b5f
reader and writer implementations
shbhmrzd 4fc6655
revert implementation details
shbhmrzd dbcbaca
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 46c1632
cargo fmt and clippy
shbhmrzd e021fd2
review comments
shbhmrzd 5b55764
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 8b9d12a
rename to error.rs
shbhmrzd 6e76a0b
fix clippy unused issue for now
shbhmrzd 6344fb9
commit cargo.lock file
shbhmrzd 15e5450
Merge remote-tracking branch 'upstream/main' into support-hdfs-native
shbhmrzd 2a2b8ce
Fix cargo lock changes
Xuanwo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| // 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, Formatter}; | ||
| use std::sync::Arc; | ||
|
|
||
| use async_trait::async_trait; | ||
| use log::debug; | ||
| use serde::Deserialize; | ||
| use uuid::Uuid; | ||
|
|
||
| use super::lister::NativeHdfsLister; | ||
| use super::reader::NativeHdfsReader; | ||
| use super::writer::NativeHdfsWriter; | ||
| use crate::raw::*; | ||
| use crate::*; | ||
|
|
||
| /// [Hadoop Distributed File System (HDFS™)](https://hadoop.apache.org/) support. | ||
| /// Using [Native Rust HDFS client](https://github.com/Kimahriman/hdfs-native). | ||
|
|
||
| /// Config for NativeHdfs services support. | ||
| #[derive(Default, Deserialize, Clone)] | ||
| #[serde(default)] | ||
| #[non_exhaustive] | ||
| pub struct HdfsNativeConfig { | ||
| /// work dir of this backend | ||
| pub root: Option<String>, | ||
| /// url of this backend | ||
| pub url: Option<String>, | ||
| /// enable the append capacity | ||
| pub enable_append: bool, | ||
| } | ||
|
|
||
| impl Debug for HdfsNativeConfig { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("HdfsNativeConfig") | ||
| .field("root", &self.root) | ||
| .field("url", &self.url) | ||
| .field("enable_append", &self.enable_append) | ||
| .finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| #[doc = include_str!("docs.md")] | ||
| #[derive(Default)] | ||
| pub struct HdfsNativeBuilder { | ||
| config: HdfsNativeConfig, | ||
| } | ||
|
|
||
| impl Debug for HdfsNativeBuilder { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("HdfsNativeBuilder") | ||
| .field("config", &self.config) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl HdfsNativeBuilder { | ||
| /// Set root of this backend. | ||
| /// | ||
| /// All operations will happen under this root. | ||
| pub fn root(&mut self, root: &str) -> &mut Self { | ||
| self.config.root = if root.is_empty() { | ||
| None | ||
| } else { | ||
| Some(root.to_string()) | ||
| }; | ||
|
|
||
| self | ||
| } | ||
|
|
||
| /// Set url of this backend. | ||
| /// | ||
| /// Valid format including: | ||
| /// | ||
| /// - `default`: using the default setting based on hadoop config. | ||
| /// - `hdfs://127.0.0.1:9000`: connect to hdfs cluster. | ||
| pub fn url(&mut self, url: &str) -> &mut Self { | ||
| if !name_node.is_empty() { | ||
| // Trim trailing `/` so that we can accept `http://127.0.0.1:9000/` | ||
| self.config.url = Some(url.trim_end_matches('/').to_string()) | ||
| } | ||
|
|
||
| self | ||
| } | ||
|
|
||
| /// Enable append capacity of this backend. | ||
| /// | ||
| /// This should be disabled when HDFS runs in non-distributed mode. | ||
| pub fn enable_append(&mut self, enable_append: bool) -> &mut Self { | ||
| self.config.enable_append = enable_append; | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Builder for HdfsNativeBuilder { | ||
| const SCHEME: Scheme = Scheme::NativeHdfs; | ||
| type Accessor = NativeHdfsBackend; | ||
|
|
||
| fn from_map(map: HashMap<String, String>) -> Self { | ||
| // Deserialize the configuration from the HashMap. | ||
| let config = HdfsNativeConfig::deserialize(ConfigDeserializer::new(map)) | ||
| .expect("config deserialize must succeed"); | ||
|
|
||
| // Create an NativeHdfsBuilder instance with the deserialized config. | ||
| HdfsNativeBuilder { config } | ||
| } | ||
|
|
||
| fn build(&mut self) -> Result<Self::Accessor> { | ||
| debug!("backend build started: {:?}", &self); | ||
|
|
||
| let url = match &self.config.url { | ||
| Some(v) => v, | ||
| None => { | ||
| return Err(Error::new(ErrorKind::ConfigInvalid, "url is empty") | ||
| .with_context("service", Scheme::NativeHdfs)) | ||
| } | ||
| }; | ||
|
|
||
| let root = normalize_root(&self.config.root.take().unwrap_or_default()); | ||
| debug!("backend use root {}", root); | ||
|
|
||
| let client = hdfs_native::Client::new(url).map_err(new_std_io_error)?; | ||
|
|
||
| // need to check if root dir exists, create if not | ||
|
|
||
| debug!("backend build finished: {:?}", &self); | ||
| Ok(NativeHdfsBackend { | ||
| root, | ||
| client: Arc::new(client), | ||
| enable_append: self.config.enable_append, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn tmp_file_of(path: &str) -> String { | ||
| let name = get_basename(path); | ||
| let uuid = Uuid::new_v4().to_string(); | ||
|
|
||
| format!("{name}.{uuid}") | ||
| } | ||
|
|
||
| /// Backend for hdfs-native services. | ||
| #[derive(Debug, Clone)] | ||
| pub struct NativeHdfsBackend { | ||
| root: String, | ||
| client: Arc<hdfs_native::Client>, | ||
| enable_append: bool, | ||
| } | ||
|
|
||
| /// hdfs_native::Client is thread-safe. | ||
| unsafe impl Send for NativeHdfsBackend {} | ||
| unsafe impl Sync for NativeHdfsBackend {} | ||
|
|
||
| #[async_trait] | ||
| impl Accessor for NativeHdfsBackend { | ||
| type Reader = NativeHdfsReader; | ||
| type BlockingReader = (); | ||
| type Writer = NativeHdfsWriter; | ||
| type BlockingWriter = (); | ||
| type Lister = Option<NativeHdfsLister>; | ||
| type BlockingLister = (); | ||
|
|
||
| fn info(&self) -> AccessorInfo { | ||
| todo!() | ||
| } | ||
|
|
||
| async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> { | ||
| let p = build_rooted_abs_path(&self.root, path); | ||
|
|
||
| self.client | ||
| .mkdirs(&p, 0o777, true) | ||
| .await | ||
| .map_err(new_std_io_error)?; | ||
| Ok(RpCreateDir::default()) | ||
| } | ||
|
|
||
| async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> { | ||
| let p = build_rooted_abs_path(&self.root, path); | ||
|
|
||
| let f = self.client.read(&p).await.map_err(new_std_io_error)?; | ||
|
|
||
| let r = NativeHdfsReader::new(f); | ||
|
|
||
| Ok((RpRead::new(), r)) | ||
| } | ||
|
|
||
| async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> { | ||
| todo!() | ||
| } | ||
|
|
||
| async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> { | ||
| todo!() | ||
| } | ||
|
|
||
| async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> { | ||
| todo!() | ||
| } | ||
|
|
||
| async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> { | ||
| todo!() | ||
| } | ||
|
|
||
| async fn delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> { | ||
| todo!() | ||
| } | ||
|
|
||
| async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> { | ||
| let p = build_rooted_abs_path(&self.root, path); | ||
| todo!() | ||
| } | ||
| } |
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,82 @@ | ||
| A distributed file system that provides high-throughput access to application data. | ||
|
|
||
| ## Capabilities | ||
|
|
||
| This service can be used to: | ||
|
|
||
| - [x] stat | ||
| - [x] read | ||
| - [x] write | ||
| - [x] create_dir | ||
| - [x] delete | ||
| - [ ] copy | ||
| - [x] rename | ||
| - [x] list | ||
| - [ ] ~~scan~~ | ||
| - [ ] ~~presign~~ | ||
| - [x] blocking | ||
| - [x] append | ||
|
|
||
| ## Differences with webhdfs | ||
|
|
||
| [Webhdfs][crate::services::Webhdfs] is powered by hdfs's RESTful HTTP API. | ||
|
|
||
| ## Features | ||
|
|
||
| Native HDFS support needs to enable feature `services-native-hdfs`. | ||
|
|
||
| ## Configuration | ||
|
|
||
| - `root`: Set the work dir for backend. | ||
| - `name_node`: Set the name node for backend. | ||
| - `enable_append`: enable the append capacity. Default is false. | ||
|
|
||
| Refer to [`HdfsNativeBuilder`]'s public API docs for more information. | ||
|
|
||
| ## Environment | ||
|
|
||
| If HDFS has High Availability (HA) enabled with multiple available NameNodes, some configuration is required: | ||
| 1. Obtain the entire HDFS config folder (usually located at HADOOP_HOME/etc/hadoop). | ||
| 2. Set the environment variable HADOOP_CONF_DIR to the path of this folder. | ||
| ```shell | ||
| export HADOOP_CONF_DIR=<path of the config folder> | ||
| ``` | ||
| 3. Use the `cluster_name` specified in the `core-site.xml` file (located in the HADOOP_CONF_DIR folder) to replace namenode:port. | ||
|
|
||
| ```rust | ||
| builder.name_node("hdfs://cluster_name"); | ||
| ``` | ||
|
|
||
| ## Example | ||
|
|
||
| ### Via Builder | ||
|
|
||
| ```rust | ||
| use std::sync::Arc; | ||
|
|
||
| use anyhow::Result; | ||
| use opendal::services::Hdfs; | ||
| use opendal::Operator; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<()> { | ||
| // Create native hdfs backend builder. | ||
| let mut builder = HdfsNative::default(); | ||
|
Xuanwo marked this conversation as resolved.
Outdated
|
||
| // Set the url for hdfs. | ||
| builder.name_node("hdfs://127.0.0.1:9000"); | ||
| // Set the root for hdfs, all operations will happen under this root. | ||
| // | ||
| // NOTE: the root must be absolute path. | ||
| builder.root("/tmp"); | ||
|
|
||
| // Enable the append capacity for hdfs. | ||
| // | ||
| // Note: HDFS run in non-distributed mode doesn't support append. | ||
| builder.enable_append(true); | ||
|
|
||
| // `Accessor` provides the low level APIs, we will use `Operator` normally. | ||
| let op: Operator = Operator::new(builder)?.finish(); | ||
|
|
||
| Ok(()) | ||
| } | ||
| ``` | ||
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,39 @@ | ||
| // 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 crate::raw::oio; | ||
| use crate::raw::oio::Entry; | ||
| use crate::*; | ||
| use std::sync::Arc; | ||
| use std::task::{Context, Poll}; | ||
|
|
||
| pub struct HdfsNativeLister { | ||
| root: String, | ||
| client: Arc<hdfs_native::Client>, | ||
| } | ||
|
|
||
| impl HdfsNativeLister { | ||
| pub fn new(path: String, client: Arc<hdfs_native::Client>) -> Self { | ||
| HdfsNativeLister { root: path, client } | ||
| } | ||
| } | ||
|
|
||
| impl oio::List for HdfsNativeLister { | ||
| fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<Entry>>> { | ||
| todo!() | ||
| } | ||
| } |
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,24 @@ | ||
| // 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. | ||
|
|
||
| mod backend; | ||
| pub use backend::HdfsNativeBuilder as HdfsNative; | ||
| pub use backend::HdfsNativeConfig; | ||
|
|
||
| mod lister; | ||
| mod reader; | ||
| mod writer; |
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.