-
Notifications
You must be signed in to change notification settings - Fork 0
feat: auto-detect proxy and translate peer addresses to contact point #153
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
Open
fruch
wants to merge
2
commits into
main
Choose a base branch
from
feat/proxy-auto-detect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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,57 @@ | ||
| //! Proxy address translator. | ||
| //! | ||
| //! When connecting through a proxy or load balancer (e.g., AWS NLB, PrivateLink), | ||
| //! the driver discovers internal node IPs from `system.peers` that are unreachable | ||
| //! from the client. This translator remaps all peer addresses to the original | ||
| //! contact point, ensuring all connections go through the proxy. | ||
|
|
||
| use std::net::SocketAddr; | ||
|
|
||
| use async_trait::async_trait; | ||
| use scylla::errors::TranslationError; | ||
| use scylla::policies::address_translator::{AddressTranslator, UntranslatedPeer}; | ||
|
|
||
| /// An [`AddressTranslator`] that redirects all peer connections to the original | ||
| /// contact point address. Used when the cluster is accessed through a proxy. | ||
| /// | ||
| /// All discovered node addresses are translated to `proxy_address`, ensuring | ||
| /// the driver only connects through the proxy endpoint. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ProxyAddressTranslator { | ||
| /// The proxy/contact point address to route all connections through. | ||
| proxy_address: SocketAddr, | ||
| } | ||
|
|
||
| impl ProxyAddressTranslator { | ||
| /// Create a new translator that routes all connections to `proxy_address`. | ||
| pub fn new(proxy_address: SocketAddr) -> Self { | ||
| Self { proxy_address } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl AddressTranslator for ProxyAddressTranslator { | ||
| async fn translate_address( | ||
| &self, | ||
| _untranslated_peer: &UntranslatedPeer, | ||
| ) -> Result<SocketAddr, TranslationError> { | ||
| Ok(self.proxy_address) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::net::{IpAddr, Ipv4Addr}; | ||
|
|
||
| fn sock(ip: [u8; 4], port: u16) -> SocketAddr { | ||
| SocketAddr::new(IpAddr::V4(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3])), port) | ||
| } | ||
|
|
||
| #[test] | ||
| fn creates_with_correct_address() { | ||
| let proxy_addr = sock([18, 208, 144, 200], 9042); | ||
| let translator = ProxyAddressTranslator::new(proxy_addr); | ||
| assert_eq!(translator.proxy_address, proxy_addr); | ||
| } | ||
| } | ||
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
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,91 @@ | ||
| //! Integration tests for proxy auto-detection. | ||
| //! | ||
| //! Tests that verify the proxy address translator works correctly with a | ||
| //! real ScyllaDB instance. | ||
|
|
||
| use helpers::{cqlsh_cmd, get_scylla}; | ||
|
|
||
| use super::*; | ||
|
|
||
| /// Direct connection (no proxy) should work without address translation. | ||
| /// This confirms the two-phase connect doesn't break normal connections. | ||
| #[test] | ||
| #[ignore = "requires Docker"] | ||
| fn test_direct_connection_still_works_with_proxy_detection() { | ||
| let scylla = get_scylla(); | ||
| cqlsh_cmd(scylla) | ||
| .args(["-e", "SELECT cluster_name FROM system.local"]) | ||
| .assert() | ||
| .success(); | ||
| } | ||
|
|
||
| /// Verify that a query actually returns data through the normal path, | ||
| /// confirming the two-phase connect doesn't cause session issues. | ||
| #[test] | ||
| #[ignore = "requires Docker"] | ||
| fn test_query_after_proxy_detection_returns_data() { | ||
| let scylla = get_scylla(); | ||
| let output = | ||
| helpers::execute_cql_output_direct(scylla, "SELECT release_version FROM system.local"); | ||
| assert!( | ||
| !output.is_empty(), | ||
| "expected output from system.local query" | ||
| ); | ||
| } | ||
|
|
||
| /// Test connection through a TCP proxy (socat) to simulate the proxy scenario. | ||
| /// The proxy forwards traffic to the ScyllaDB container, but its IP won't | ||
| /// match any peer address in system.peers, triggering proxy auto-detection. | ||
| #[test] | ||
| #[ignore = "requires Docker"] | ||
| fn test_proxy_connection_via_socat() { | ||
| use std::process::{Command, Stdio}; | ||
| use std::thread; | ||
| use std::time::Duration; | ||
|
|
||
| let scylla = get_scylla(); | ||
| let target = format!("{}:{}", scylla.host, scylla.port); | ||
|
|
||
| // Start socat as a TCP proxy on a random high port | ||
| // socat listens on 0.0.0.0:0 (kernel picks port) and forwards to the ScyllaDB container | ||
| let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); | ||
| let proxy_port = listener.local_addr().unwrap().port(); | ||
| drop(listener); // free the port for socat | ||
|
|
||
| let mut socat = match Command::new("socat") | ||
| .args([ | ||
| &format!("TCP-LISTEN:{proxy_port},fork,reuseaddr"), | ||
| &format!("TCP:{target}"), | ||
| ]) | ||
| .stdout(Stdio::null()) | ||
| .stderr(Stdio::null()) | ||
| .spawn() | ||
| { | ||
| Ok(child) => child, | ||
| Err(_) => { | ||
| eprintln!("socat not available, skipping proxy integration test"); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| // Give socat time to start listening | ||
| thread::sleep(Duration::from_millis(500)); | ||
|
|
||
| // Connect through the proxy — proxy IP (127.0.0.1:proxy_port) won't match | ||
| // the ScyllaDB node's internal address, triggering proxy detection | ||
| let result = assert_cmd::Command::cargo_bin("cqlsh-rs") | ||
| .unwrap() | ||
| .args([ | ||
| "127.0.0.1", | ||
| &proxy_port.to_string(), | ||
| "-e", | ||
| "SELECT cluster_name FROM system.local", | ||
| ]) | ||
| .timeout(Duration::from_secs(15)) | ||
| .assert(); | ||
|
|
||
| socat.kill().ok(); | ||
| socat.wait().ok(); | ||
|
|
||
| result.success(); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this achieves what you want. I assume the proxy address leads to one specific node.
Let's say you have 5 nodes in the cluster, each with 32 shards. With your changes, the driver will still see all 5 nodes in system.peers/local, try to open connection pools to all 5 nodes. The address translation will cause all connections to be opened to the same node (driver won't even know about it), so you'll get 160 connections to this node.
You could use
PoolSize::PerNode(1)(which is a good idea in cqlsh regardless of all other changes) to get this down to 5.Then the connection amount problem is not that bad, but you are still in a very weird state where driver thinks it opened pools to all nodes, but they are really all to one node. Will this work correctly? It may, I'm not completely sure.
TBH I don't know how to solve that will existing APIs. There isn't really a way to implement a HostFilter that would filter out other nodes, because HostFilter accepts Peer, which has an address fetched from system.peers or system.local - so you can't really say for sure if its the same peer as the contact point.
What would be nice here is a simplified session, with a separate builder, where driver only opens a single connection to the given address, and uses it both as CC and to execute user requests. This would also work for the maintenance socket. cc @wprzytula - let's discuss this when we meet, there are some not obvious decisions when implementing such session.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on the connection count concern. Added
PoolSize::PerHost(1)— cqlsh is a single-user interactive tool so one connection per node is plenty. This brings it down to N connections (one per discovered node) all going through the proxy.Re: the simplified single-connection session — that would be ideal. For now this is a pragmatic workaround. Happy to migrate when that API exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. This is a weird state for the driver to be in, but I think it should work. @wprzytula will be available tomorrow if you want him to also take a look (maybe I am missing some potential problem).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your thoughs above @Lorak-mmk. Aside from that, maybe let's also use
SingleTargetLoadBalancingPolicyto simplify the load balancing part?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be difficult for the same reason host filtering is difficult. You would need to know which node you have CC opened to, and it can no longer be done by socket address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, the code in this PR was proven to be working as expected in the case the broadcast address isn't available (AWS public / private addresses)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyhow I'm going to merge this one, so it won't regress the expected beahvier from the python cqlsh (before the driver change that broke it)