* improve code for notify * improve code for logger and fix typo (#272) * Add GNU to build.yml (#275) * fix unzip error * fix url change error fix url change error * Simplify user experience and integrate console and endpoint Simplify user experience and integrate console and endpoint * Add gnu to build.yml * upgrade version * feat: add `cargo clippy --fix --allow-dirty` to pre-commit command (#282) Resolves #277 - Add --fix flag to automatically fix clippy warnings - Add --allow-dirty flag to run on dirty Git trees - Improves code quality in pre-commit workflow * fix: the issue where preview fails when the path length exceeds 255 characters (#280) * fix * fix: improve Windows build support and CI/CD workflow (#283) - Fix Windows zip command issue by using PowerShell Compress-Archive - Add Windows support for OSS upload with ossutil - Replace Chinese comments with English in build.yml - Fix bash syntax error in package_zip function - Improve code formatting and consistency - Update various configuration files for better cross-platform support Resolves Windows build failures in GitHub Actions. * fix: update link in README.md leading to a 404 error (#285) * add rustfs.spec for rustfs (#103) add support on loongarch64 * improve cargo.lock * build(deps): bump the dependencies group with 5 updates (#289) Bumps the dependencies group with 5 updates: | Package | From | To | | --- | --- | --- | | [hyper-util](https://github.com/hyperium/hyper-util) | `0.1.15` | `0.1.16` | | [rand](https://github.com/rust-random/rand) | `0.9.1` | `0.9.2` | | [serde_json](https://github.com/serde-rs/json) | `1.0.140` | `1.0.141` | | [strum](https://github.com/Peternator7/strum) | `0.27.1` | `0.27.2` | | [sysinfo](https://github.com/GuillaumeGomez/sysinfo) | `0.36.0` | `0.36.1` | Updates `hyper-util` from 0.1.15 to 0.1.16 - [Release notes](https://github.com/hyperium/hyper-util/releases) - [Changelog](https://github.com/hyperium/hyper-util/blob/master/CHANGELOG.md) - [Commits](https://github.com/hyperium/hyper-util/compare/v0.1.15...v0.1.16) Updates `rand` from 0.9.1 to 0.9.2 - [Release notes](https://github.com/rust-random/rand/releases) - [Changelog](https://github.com/rust-random/rand/blob/master/CHANGELOG.md) - [Commits](https://github.com/rust-random/rand/compare/rand_core-0.9.1...rand_core-0.9.2) Updates `serde_json` from 1.0.140 to 1.0.141 - [Release notes](https://github.com/serde-rs/json/releases) - [Commits](https://github.com/serde-rs/json/compare/v1.0.140...v1.0.141) Updates `strum` from 0.27.1 to 0.27.2 - [Release notes](https://github.com/Peternator7/strum/releases) - [Changelog](https://github.com/Peternator7/strum/blob/master/CHANGELOG.md) - [Commits](https://github.com/Peternator7/strum/compare/v0.27.1...v0.27.2) Updates `sysinfo` from 0.36.0 to 0.36.1 - [Changelog](https://github.com/GuillaumeGomez/sysinfo/blob/master/CHANGELOG.md) - [Commits](https://github.com/GuillaumeGomez/sysinfo/compare/v0.36.0...v0.36.1) --- updated-dependencies: - dependency-name: hyper-util dependency-version: 0.1.16 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: dependencies - dependency-name: rand dependency-version: 0.9.2 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: dependencies - dependency-name: serde_json dependency-version: 1.0.141 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: dependencies - dependency-name: strum dependency-version: 0.27.2 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: dependencies - dependency-name: sysinfo dependency-version: 0.36.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: dependencies ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * improve code for logger * improve * upgrade * refactor: 优化构建工作流,统一 latest 文件处理和简化制品上传 (#293) * Refactor: DatabaseManagerSystem as global Signed-off-by: junxiang Mu <[email protected]> * fix: fmt Signed-off-by: junxiang Mu <[email protected]> * Test: add e2e_test for s3select Signed-off-by: junxiang Mu <[email protected]> * Test: add test script for e2e Signed-off-by: junxiang Mu <[email protected]> * improve code for registry and intergation * improve code for registry `create_targets_from_config` * fix * Feature up/ilm (#305) * fix * fix * fix * fix delete-marker expiration. add api_restore. * fix * time retry object upload * lock file * make fmt * fix * restore object * fix * fix * serde-rs-xml -> quick-xml * fix * checksum * fix * fix * fix * fix * fix * fix * fix * transfer lang to english * upgrade clap version from 4.5.41 to 4.5.42 * refactor: replace `lazy_static` with `LazyLock` * add router * fix: modify comment * improve code * fix typos * fix * fix: modify name and fmt * improve code for registry * fix test --------- Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: junxiang Mu <[email protected]> Co-authored-by: loverustfs <[email protected]> Co-authored-by: 安正超 <[email protected]> Co-authored-by: shiro.lee <[email protected]> Co-authored-by: Marco Orlandin <[email protected]> Co-authored-by: zhangwenlong <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: junxiang Mu <[email protected]> Co-authored-by: likewu <[email protected]>
230 lines
7.5 KiB
Rust
230 lines
7.5 KiB
Rust
// Copyright 2024 RustFS Team
|
|
//
|
|
// Licensed 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::config::{Config, GLOBAL_STORAGE_CLASS, storageclass};
|
|
use crate::disk::RUSTFS_META_BUCKET;
|
|
use crate::error::{Error, Result};
|
|
use crate::store_api::{ObjectInfo, ObjectOptions, PutObjReader, StorageAPI};
|
|
use http::HeaderMap;
|
|
use rustfs_config::DEFAULT_DELIMITER;
|
|
use rustfs_utils::path::SLASH_SEPARATOR;
|
|
use std::collections::HashSet;
|
|
use std::sync::Arc;
|
|
use std::sync::LazyLock;
|
|
use tracing::{error, warn};
|
|
|
|
pub const CONFIG_PREFIX: &str = "config";
|
|
const CONFIG_FILE: &str = "config.json";
|
|
|
|
pub const STORAGE_CLASS_SUB_SYS: &str = "storage_class";
|
|
|
|
static CONFIG_BUCKET: LazyLock<String> = LazyLock::new(|| format!("{RUSTFS_META_BUCKET}{SLASH_SEPARATOR}{CONFIG_PREFIX}"));
|
|
|
|
static SUB_SYSTEMS_DYNAMIC: LazyLock<HashSet<String>> = LazyLock::new(|| {
|
|
let mut h = HashSet::new();
|
|
h.insert(STORAGE_CLASS_SUB_SYS.to_owned());
|
|
h
|
|
});
|
|
pub async fn read_config<S: StorageAPI>(api: Arc<S>, file: &str) -> Result<Vec<u8>> {
|
|
let (data, _obj) = read_config_with_metadata(api, file, &ObjectOptions::default()).await?;
|
|
Ok(data)
|
|
}
|
|
|
|
pub async fn read_config_with_metadata<S: StorageAPI>(
|
|
api: Arc<S>,
|
|
file: &str,
|
|
opts: &ObjectOptions,
|
|
) -> Result<(Vec<u8>, ObjectInfo)> {
|
|
let h = HeaderMap::new();
|
|
let mut rd = api
|
|
.get_object_reader(RUSTFS_META_BUCKET, file, None, h, opts)
|
|
.await
|
|
.map_err(|err| {
|
|
if err == Error::FileNotFound || matches!(err, Error::ObjectNotFound(_, _)) {
|
|
Error::ConfigNotFound
|
|
} else {
|
|
warn!("read_config_with_metadata: err: {:?}, file: {}", err, file);
|
|
err
|
|
}
|
|
})?;
|
|
|
|
let data = rd.read_all().await?;
|
|
|
|
if data.is_empty() {
|
|
return Err(Error::ConfigNotFound);
|
|
}
|
|
|
|
Ok((data, rd.object_info))
|
|
}
|
|
|
|
pub async fn save_config<S: StorageAPI>(api: Arc<S>, file: &str, data: Vec<u8>) -> Result<()> {
|
|
save_config_with_opts(
|
|
api,
|
|
file,
|
|
data,
|
|
&ObjectOptions {
|
|
max_parity: true,
|
|
..Default::default()
|
|
},
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn delete_config<S: StorageAPI>(api: Arc<S>, file: &str) -> Result<()> {
|
|
match api
|
|
.delete_object(
|
|
RUSTFS_META_BUCKET,
|
|
file,
|
|
ObjectOptions {
|
|
delete_prefix: true,
|
|
delete_prefix_object: true,
|
|
..Default::default()
|
|
},
|
|
)
|
|
.await
|
|
{
|
|
Ok(_) => Ok(()),
|
|
Err(err) => {
|
|
if err == Error::FileNotFound || matches!(err, Error::ObjectNotFound(_, _)) {
|
|
Err(Error::ConfigNotFound)
|
|
} else {
|
|
Err(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn save_config_with_opts<S: StorageAPI>(api: Arc<S>, file: &str, data: Vec<u8>, opts: &ObjectOptions) -> Result<()> {
|
|
if let Err(err) = api
|
|
.put_object(RUSTFS_META_BUCKET, file, &mut PutObjReader::from_vec(data), opts)
|
|
.await
|
|
{
|
|
error!("save_config_with_opts: err: {:?}, file: {}", err, file);
|
|
return Err(err);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn new_server_config() -> Config {
|
|
Config::new()
|
|
}
|
|
|
|
async fn new_and_save_server_config<S: StorageAPI>(api: Arc<S>) -> Result<Config> {
|
|
let mut cfg = new_server_config();
|
|
lookup_configs(&mut cfg, api.clone()).await;
|
|
save_server_config(api, &cfg).await?;
|
|
|
|
Ok(cfg)
|
|
}
|
|
|
|
fn get_config_file() -> String {
|
|
format!("{CONFIG_PREFIX}{SLASH_SEPARATOR}{CONFIG_FILE}")
|
|
}
|
|
|
|
/// Handle the situation where the configuration file does not exist, create and save a new configuration
|
|
async fn handle_missing_config<S: StorageAPI>(api: Arc<S>, context: &str) -> Result<Config> {
|
|
warn!("Configuration not found ({}): Start initializing new configuration", context);
|
|
let cfg = new_and_save_server_config(api).await?;
|
|
warn!("Configuration initialization complete ({})", context);
|
|
Ok(cfg)
|
|
}
|
|
|
|
/// Handle configuration file read errors
|
|
fn handle_config_read_error(err: Error, file_path: &str) -> Result<Config> {
|
|
error!("Read configuration failed (path: '{}'): {:?}", file_path, err);
|
|
Err(err)
|
|
}
|
|
|
|
pub async fn read_config_without_migrate<S: StorageAPI>(api: Arc<S>) -> Result<Config> {
|
|
let config_file = get_config_file();
|
|
|
|
// Try to read the configuration file
|
|
match read_config(api.clone(), &config_file).await {
|
|
Ok(data) => read_server_config(api, &data).await,
|
|
Err(Error::ConfigNotFound) => handle_missing_config(api, "Read the main configuration").await,
|
|
Err(err) => handle_config_read_error(err, &config_file),
|
|
}
|
|
}
|
|
|
|
async fn read_server_config<S: StorageAPI>(api: Arc<S>, data: &[u8]) -> Result<Config> {
|
|
// If the provided data is empty, try to read from the file again
|
|
if data.is_empty() {
|
|
let config_file = get_config_file();
|
|
warn!("Received empty configuration data, try to reread from '{}'", config_file);
|
|
|
|
// Try to read the configuration again
|
|
match read_config(api.clone(), &config_file).await {
|
|
Ok(cfg_data) => {
|
|
// TODO: decrypt
|
|
let cfg = Config::unmarshal(&cfg_data)?;
|
|
return Ok(cfg.merge());
|
|
}
|
|
Err(Error::ConfigNotFound) => return handle_missing_config(api, "Read alternate configuration").await,
|
|
Err(err) => return handle_config_read_error(err, &config_file),
|
|
}
|
|
}
|
|
|
|
// Process non-empty configuration data
|
|
let cfg = Config::unmarshal(data)?;
|
|
Ok(cfg.merge())
|
|
}
|
|
|
|
pub async fn save_server_config<S: StorageAPI>(api: Arc<S>, cfg: &Config) -> Result<()> {
|
|
let data = cfg.marshal()?;
|
|
|
|
let config_file = get_config_file();
|
|
|
|
save_config(api, &config_file, data).await
|
|
}
|
|
|
|
pub async fn lookup_configs<S: StorageAPI>(cfg: &mut Config, api: Arc<S>) {
|
|
// TODO: from etcd
|
|
if let Err(err) = apply_dynamic_config(cfg, api).await {
|
|
error!("apply_dynamic_config err {:?}", &err);
|
|
}
|
|
}
|
|
|
|
async fn apply_dynamic_config<S: StorageAPI>(cfg: &mut Config, api: Arc<S>) -> Result<()> {
|
|
for key in SUB_SYSTEMS_DYNAMIC.iter() {
|
|
apply_dynamic_config_for_sub_sys(cfg, api.clone(), key).await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn apply_dynamic_config_for_sub_sys<S: StorageAPI>(cfg: &mut Config, api: Arc<S>, subsys: &str) -> Result<()> {
|
|
let set_drive_counts = api.set_drive_counts();
|
|
if subsys == STORAGE_CLASS_SUB_SYS {
|
|
let kvs = cfg.get_value(STORAGE_CLASS_SUB_SYS, DEFAULT_DELIMITER).unwrap_or_default();
|
|
|
|
for (i, count) in set_drive_counts.iter().enumerate() {
|
|
match storageclass::lookup_config(&kvs, *count) {
|
|
Ok(res) => {
|
|
if i == 0 && GLOBAL_STORAGE_CLASS.get().is_none() {
|
|
if let Err(r) = GLOBAL_STORAGE_CLASS.set(res) {
|
|
error!("GLOBAL_STORAGE_CLASS.set failed {:?}", r);
|
|
}
|
|
}
|
|
}
|
|
Err(err) => {
|
|
error!("init storage class err:{:?}", &err);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|