Skip to content
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

feat(query): Fail fast if the underlying storage is not available #5671

Merged
merged 2 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion common/exception/src/exception_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ build_exceptions! {
build_exceptions! {
StorageNotFound(3001),
StoragePermissionDenied(3002),
StorageOther(4000)
StorageUnavailable(3901),
StorageOther(4000),
}

// Cache errors [4001, 5000].
Expand Down
12 changes: 10 additions & 2 deletions query/src/sessions/session_mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,17 @@ impl SessionManager {
// Init the storage operator by config.
async fn init_storage_operator(conf: &Config) -> Result<Operator> {
let op = init_operator(&conf.storage).await?;

// Enable exponential backoff by default
Ok(op.with_backoff(backon::ExponentialBackoff::default()))
let op = op.with_backoff(backon::ExponentialBackoff::default());
// OpenDAL will send a real request to underlying storage to check whether it works or not.
// If this check failed, it's highly possible that the users have configured it wrongly.
op.check().await.map_err(|e| {
ErrorCode::StorageUnavailable(format!(
"current configured storage is not available: {e}"
))
})?;

Ok(op)
}

pub async fn reload_config(&self) -> Result<()> {
Expand Down
15 changes: 13 additions & 2 deletions query/tests/it/sessions/query_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ use common_exception::Result;
use common_io::prelude::StorageFsConfig;
use common_io::prelude::StorageParams;
use common_io::prelude::StorageS3Config;
use wiremock::matchers::method;
use wiremock::matchers::path;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
// This test need network
async fn test_get_storage_accessor_s3() -> Result<()> {
let mock_server = MockServer::start().await;
Mock::given(method("HEAD"))
.and(path("/bucket/.opendal"))
.respond_with(ResponseTemplate::new(404))
.mount(&mock_server)
.await;

let mut conf = crate::tests::ConfigBuilder::create().config();

conf.storage.params = StorageParams::S3(StorageS3Config {
region: "us-east-2".to_string(),
endpoint_url: "http://s3.amazonaws.com".to_string(),
endpoint_url: mock_server.uri(),
access_key_id: "".to_string(),
secret_access_key: "".to_string(),
bucket: "bucket".to_string(),
Expand Down
24 changes: 22 additions & 2 deletions query/tests/it/storages/system/configs_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ use databend_query::storages::system::ConfigsTable;
use databend_query::storages::ToReadDataSourcePlan;
use futures::TryStreamExt;
use pretty_assertions::assert_eq;
use wiremock::matchers::method;
use wiremock::matchers::path;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_configs_table() -> Result<()> {
Expand Down Expand Up @@ -112,8 +117,17 @@ async fn test_configs_table() -> Result<()> {

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_configs_table_redact() -> Result<()> {
let mock_server = MockServer::start().await;
Mock::given(method("HEAD"))
.and(path("/test/.opendal"))
.respond_with(ResponseTemplate::new(404))
.mount(&mock_server)
.await;

let mut conf = crate::tests::ConfigBuilder::create().config();
conf.storage.params = StorageParams::S3(StorageS3Config {
region: "us-east-2".to_string(),
endpoint_url: mock_server.uri(),
bucket: "test".to_string(),
access_key_id: "access_key_id".to_string(),
secret_access_key: "secret_access_key".to_string(),
Expand All @@ -130,6 +144,11 @@ async fn test_configs_table_redact() -> Result<()> {
let block = &result[0];
assert_eq!(block.num_columns(), 4);

let endpoint_url_link = format!(
"| storage | s3.endpoint_url | {:<24} | |",
mock_server.uri()
);

let expected = vec![
"+---------+--------------------------------------+---------------------------+-------------+",
"| group | name | value | description |",
Expand Down Expand Up @@ -193,14 +212,15 @@ async fn test_configs_table_redact() -> Result<()> {
"| storage | num_cpus | 0 | |",
"| storage | s3.access_key_id | ******_id | |",
"| storage | s3.bucket | test | |",
"| storage | s3.endpoint_url | https://s3.amazonaws.com | |",
&endpoint_url_link,
"| storage | s3.master_key | | |",
"| storage | s3.region | | |",
"| storage | s3.region | us-east-2 | |",
"| storage | s3.root | | |",
"| storage | s3.secret_access_key | ******key | |",
"| storage | type | s3 | |",
"+---------+--------------------------------------+---------------------------+-------------+",
];

common_datablocks::assert_blocks_sorted_eq(expected, result.as_slice());
Ok(())
}