|
| 1 | +use crate::utils::{test_with_3_node_cluster, unique_keyspace_name, PerformDDL}; |
| 2 | +use scylla::client::session::Session; |
| 3 | +use scylla::client::session_builder::SessionBuilder; |
| 4 | +use scylla::frame::Compression; |
| 5 | +use scylla::statement::query::Query; |
| 6 | + |
| 7 | +use scylla_proxy::{ |
| 8 | + Condition, ProxyError, Reaction, RequestReaction, RequestRule, ShardAwareness, |
| 9 | + WorkerError, |
| 10 | +}; |
| 11 | + |
| 12 | +use std::sync::Arc; |
| 13 | +use tokio::sync::mpsc; |
| 14 | + |
| 15 | +/// Tests the compression functionality of the Scylla driver by performing a series of operations |
| 16 | +/// on a 3-node cluster with optional compression and verifying the total frame size of the requests. |
| 17 | +/// |
| 18 | +/// # Arguments |
| 19 | +/// |
| 20 | +/// * `compression` - An optional `Compression` enum value specifying the type of compression to use. |
| 21 | +/// * `text_size` - The size of the text to be inserted into the test table. |
| 22 | +/// * `expected_frame_total_size_range` - A range specifying the expected total size of the frames. |
| 23 | +/// |
| 24 | +/// # Panics |
| 25 | +/// |
| 26 | +/// This function will panic if the total frame size does not fall within the expected range or if |
| 27 | +/// any of the operations (such as creating keyspace, table, or inserting/querying data) fail. |
| 28 | +async fn test_compression( |
| 29 | + compression: Option<Compression>, |
| 30 | + text_size: usize, |
| 31 | + expected_frame_total_size_range: std::ops::Range<usize>, |
| 32 | +) { |
| 33 | + let res = test_with_3_node_cluster(ShardAwareness::QueryNode, |proxy_uris, translation_map, mut running_proxy| async move { |
| 34 | + |
| 35 | + let request_rule = |tx| { |
| 36 | + RequestRule( |
| 37 | + Condition::True, |
| 38 | + RequestReaction::noop().with_feedback_when_performed(tx), |
| 39 | + ) |
| 40 | + }; |
| 41 | + |
| 42 | + let (request_tx, mut request_rx) = mpsc::unbounded_channel(); |
| 43 | + for running_node in running_proxy.running_nodes.iter_mut() { |
| 44 | + running_node.change_request_rules(Some(vec![request_rule(request_tx.clone())])); |
| 45 | + } |
| 46 | + |
| 47 | + let session: Session = SessionBuilder::new() |
| 48 | + .known_node(proxy_uris[0].as_str()) |
| 49 | + .address_translator(Arc::new(translation_map)) |
| 50 | + .compression(compression) |
| 51 | + .build() |
| 52 | + .await |
| 53 | + .unwrap(); |
| 54 | + |
| 55 | + let ks = unique_keyspace_name(); |
| 56 | + session.ddl(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 3}}", ks)).await.unwrap(); |
| 57 | + session.use_keyspace(ks, false).await.unwrap(); |
| 58 | + session |
| 59 | + .ddl("CREATE TABLE test (k text PRIMARY KEY, t text, i int, f float)") |
| 60 | + .await |
| 61 | + .unwrap(); |
| 62 | + |
| 63 | + let q = Query::from("INSERT INTO test (k, t, i, f) VALUES (?, ?, ?, ?)"); |
| 64 | + let large_string = "a".repeat(text_size); |
| 65 | + session.query_unpaged(q.clone(), ("key", large_string.as_str(), 42_i32, 24.03_f32)).await.unwrap(); |
| 66 | + |
| 67 | + let result: Vec<(String, String, i32, f32)> = session |
| 68 | + .query_unpaged("SELECT k, t, i, f FROM test WHERE k = 'key'", &[]) |
| 69 | + .await |
| 70 | + .unwrap() |
| 71 | + .into_rows_result() |
| 72 | + .unwrap() |
| 73 | + .rows::<(String, String, i32, f32)>() |
| 74 | + .unwrap() |
| 75 | + .collect::<Result<_, _>>() |
| 76 | + .unwrap(); |
| 77 | + |
| 78 | + assert_eq!(result, vec![((String::from("key"), String::from(large_string), 42_i32, 24.03_f32))]); |
| 79 | + |
| 80 | + |
| 81 | + let mut total_frame_size = 0; |
| 82 | + while let Ok((request_frame, _shard)) = request_rx.try_recv() { |
| 83 | + total_frame_size += request_frame.body.len(); |
| 84 | + } |
| 85 | + println!("Total frame size: {}", total_frame_size); |
| 86 | + assert!(expected_frame_total_size_range.contains(&total_frame_size)); |
| 87 | + |
| 88 | + running_proxy |
| 89 | + |
| 90 | + }).await; |
| 91 | + |
| 92 | + match res { |
| 93 | + Ok(()) => (), |
| 94 | + Err(ProxyError::Worker(WorkerError::DriverDisconnected(_))) => (), |
| 95 | + Err(err) => panic!("{}", err), |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[tokio::test] |
| 100 | +#[cfg(not(scylla_cloud_tests))] |
| 101 | +async fn should_execute_queries_without_compression() { |
| 102 | + test_compression(None, 1024, 4200..9000).await; |
| 103 | +} |
| 104 | + |
| 105 | +#[tokio::test] |
| 106 | +#[cfg(not(scylla_cloud_tests))] |
| 107 | +async fn should_execute_queries_without_compression_10mb() { |
| 108 | + test_compression(None, 1024 * 10, 14300..19000).await; |
| 109 | +} |
| 110 | + |
| 111 | +#[tokio::test] |
| 112 | +#[cfg(not(scylla_cloud_tests))] |
| 113 | +async fn should_execute_queries_with_snappy_compression() { |
| 114 | + test_compression(Some(Compression::Snappy), 1024, 3100..11000).await; |
| 115 | +} |
| 116 | + |
| 117 | +#[tokio::test] |
| 118 | +#[cfg(not(scylla_cloud_tests))] |
| 119 | +async fn should_execute_queries_with_snappy_compression_10mb() { |
| 120 | + test_compression(Some(Compression::Snappy), 1024 * 10, 3300..11000).await; |
| 121 | +} |
| 122 | + |
| 123 | +#[tokio::test] |
| 124 | +#[cfg(not(scylla_cloud_tests))] |
| 125 | +async fn should_execute_queries_with_lz4_compression() { |
| 126 | + test_compression(Some(Compression::Lz4), 1024, 3100..11000).await; |
| 127 | +} |
| 128 | + |
| 129 | +#[tokio::test] |
| 130 | +#[cfg(not(scylla_cloud_tests))] |
| 131 | +async fn should_execute_queries_with_lz4_compression_10mb() { |
| 132 | + test_compression(Some(Compression::Lz4), 1024 * 10, 3300..11000).await; |
| 133 | +} |
0 commit comments