Skip to content

Commit

Permalink
create a new query error called NoKnownNodeFoundError that is returne…
Browse files Browse the repository at this point in the history
…d when there are no known nodes found during query plan and avoid ambiguity
  • Loading branch information
samuelorji committed Oct 8, 2023
1 parent e6d6d3e commit 69c0a67
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
9 changes: 9 additions & 0 deletions scylla-cql/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub enum QueryError {
#[error("Request timeout: {0}")]
RequestTimeout(String),

/// No known node found to perform query
#[error("No known node found: {0}")]
NoKnownNodeFoundError(String),

/// Address translation failed
#[error("Address translation failed: {0}")]
TranslationError(#[from] TranslationError),
Expand Down Expand Up @@ -400,6 +404,10 @@ pub enum NewSessionError {
#[error("Client timeout: {0}")]
RequestTimeout(String),

/// No known node found to perform query
#[error("No known node found: {0}")]
NoKnownNodeFoundError(String),

/// Address translation failed
#[error("Address translation failed: {0}")]
TranslationError(#[from] TranslationError),
Expand Down Expand Up @@ -478,6 +486,7 @@ impl From<QueryError> for NewSessionError {
QueryError::UnableToAllocStreamId => NewSessionError::UnableToAllocStreamId,
QueryError::RequestTimeout(msg) => NewSessionError::RequestTimeout(msg),
QueryError::TranslationError(e) => NewSessionError::TranslationError(e),
QueryError::NoKnownNodeFoundError(e) => NewSessionError::NoKnownNodeFoundError(e)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions scylla/src/transport/load_balancing/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,7 @@ mod latency_awareness {
| QueryError::IoError(_)
| QueryError::ProtocolError(_)
| QueryError::TimeoutError
| QueryError::NoKnownNodeFoundError(_)
| QueryError::RequestTimeout(_) => true,
}
}
Expand Down
3 changes: 2 additions & 1 deletion scylla/src/transport/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,8 @@ impl Session {
QueryFut: Future<Output = Result<ResT, QueryError>>,
ResT: AllowedRunQueryResTType,
{
let mut last_error: Option<QueryError> = None;
// set default error as no known found as the query plan returns an empty iterator if there are no nodes in the plan
let mut last_error: Option<QueryError> = Some(QueryError::NoKnownNodeFoundError("Please confirm the supplied datacenters exists".to_string()));
let mut current_consistency: Consistency = context
.consistency_set_on_statement
.unwrap_or(execution_profile.consistency);
Expand Down
30 changes: 30 additions & 0 deletions scylla/src/transport/session_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::net::TcpListener;
use uuid::Uuid;
use crate::load_balancing::DefaultPolicy;

#[tokio::test]
async fn test_connection_failure() {
Expand Down Expand Up @@ -2857,3 +2858,32 @@ async fn test_manual_primary_key_computation() {
.await;
}
}

#[tokio::test]
async fn test_non_existent_dc_return_correct_error() {
let ks = "iot";

let mut host = "127.0.0.1";
let mut dc = "non existent dc";

let default_policy = DefaultPolicy::builder()
.prefer_datacenter(dc.to_string())
.build();

let profile = ExecutionProfile::builder()
.load_balancing_policy(default_policy)
.build();

let handle = profile.into_handle();

let session: Session = SessionBuilder::new()
.known_node(host)
.default_execution_profile_handle(handle)
.build()
.await.expect("cannot create session");

let ks_stmt = format!("CREATE KEYSPACE IF NOT EXISTS {} WITH replication = {{'class': 'NetworkTopologyStrategy', '{}': 1}}", ks, dc);
let query_result = session.query(ks_stmt, &[]).await;

assert_matches!(query_result.unwrap_err(), QueryError::NoKnownNodeFoundError(_))
}

0 comments on commit 69c0a67

Please sign in to comment.