diff --git a/docs/config.md b/docs/config.md index db5fc083a5f..5e69fc36507 100644 --- a/docs/config.md +++ b/docs/config.md @@ -99,6 +99,27 @@ time the configuration is changed to make sure that the connection pools are what is expected. Here, `$all_nodes` should be a list of all the node names that will use this configuration file. +### Setting the `fdw_host` + +Each shard's database reaches the other shards' databases through +`postgres_fdw`, and `graph-node` sets those connections up with the host and +port from each shard's `connection`. If `graph-node` reaches a database at an +address the databases themselves can't use, for example through a connection +pooler or a private endpoint that exists only in `graph-node`'s network, set +the address the databases should use with `fdw_host`, and `fdw_port` if the +port differs: + +```toml +[store.vip] +connection = "postgresql://graph:${PGPASSWORD}@vip-endpoint:6432/graph" +pool_size = 10 +fdw_host = "10.0.0.5" +fdw_port = 5432 +``` + +Both default to the host and port in `connection`. The user, password and +database name always come from `connection`. + ## Configuring Chains The `[chains]` section controls the providers that `graph-node` diff --git a/node/src/config.rs b/node/src/config.rs index 4c9b776e43a..3842173642b 100644 --- a/node/src/config.rs +++ b/node/src/config.rs @@ -355,6 +355,15 @@ pub struct Shard { pub pool_size: PoolSize, #[serde(default = "PoolSize::five")] pub fdw_pool_size: PoolSize, + /// The host the other shards' databases connect to when they reach + /// this shard through `postgres_fdw`. Defaults to the host in + /// `connection` + #[serde(default)] + pub fdw_host: Option, + /// The port that goes with `fdw_host`. Defaults to the port in + /// `connection` + #[serde(default)] + pub fdw_port: Option, #[serde(default)] pub replicas: BTreeMap, } @@ -364,6 +373,9 @@ impl Shard { ShardName::new(name.to_string()).map_err(|e| anyhow!(e))?; self.expand_connection(node)?; + if let Some(host) = &self.fdw_host { + self.fdw_host = Some(shellexpand::env(host)?.into_owned()); + } if matches!(self.pool_size, PoolSize::None) { return Err(anyhow!("missing pool size definition for shard `{}`", name)); @@ -409,6 +421,8 @@ impl Shard { weight: opt.postgres_host_weights.first().cloned().unwrap_or(1), pool_size, fdw_pool_size: PoolSize::five(), + fdw_host: None, + fdw_port: None, replicas, }) } @@ -2308,6 +2322,34 @@ fdw_pool_size = [ assert_eq!(shard.fdw_pool_size.size_for(&other, "ashard").unwrap(), 5); } + #[test] + fn fdw_address() { + let node = NodeId::new("index_node_1").unwrap(); + let mut shard = toml::from_str::( + r#" + connection = "postgresql://graph:pw@client-endpoint:6432/graph" + pool_size = 10 + fdw_host = "10.0.0.5" + fdw_port = 5432 + "#, + ) + .unwrap(); + shard.validate("shard_1", &node).unwrap(); + assert_eq!(shard.fdw_host.as_deref(), Some("10.0.0.5")); + assert_eq!(shard.fdw_port, Some(5432)); + + let mut shard = toml::from_str::( + r#" + connection = "postgresql://graph:pw@client-endpoint/graph" + pool_size = 10 + "#, + ) + .unwrap(); + shard.validate("shard_1", &node).unwrap(); + assert_eq!(shard.fdw_host, None); + assert_eq!(shard.fdw_port, None); + } + #[test] fn amp_chain_names_parsed_from_toml() { let actual: Chain = toml::from_str( diff --git a/node/src/store_builder.rs b/node/src/store_builder.rs index fb3737e5b89..b155f5a84ff 100644 --- a/node/src/store_builder.rs +++ b/node/src/store_builder.rs @@ -107,7 +107,11 @@ impl StoreBuilder { let servers = config .stores .iter() - .map(|(name, shard)| ForeignServer::new_from_raw(name.to_string(), &shard.connection)) + .map(|(name, shard)| { + ForeignServer::new_from_raw(name.to_string(), &shard.connection).map(|server| { + server.with_fdw_address(shard.fdw_host.as_deref(), shard.fdw_port) + }) + }) .collect::, _>>() .expect("connection url's contain enough detail"); let servers = Arc::new(servers); diff --git a/store/postgres/src/pool/foreign_server.rs b/store/postgres/src/pool/foreign_server.rs index c2883eb929b..d5807a35a18 100644 --- a/store/postgres/src/pool/foreign_server.rs +++ b/store/postgres/src/pool/foreign_server.rs @@ -57,6 +57,22 @@ impl ForeignServer { Self::new(Shard::new(shard)?, postgres_url) } + /// Use `host` and `port`, where given, instead of the ones from the + /// connection string as the address other shards' databases connect to + /// through `postgres_fdw`. This matters when `graph-node` reaches a + /// database at an address that the databases themselves can't use, for + /// example a connection pooler or a private endpoint that only exists + /// in `graph-node`'s network + pub fn with_fdw_address(mut self, host: Option<&str>, port: Option) -> Self { + if let Some(host) = host { + self.host = host.to_string(); + } + if let Some(port) = port { + self.port = port; + } + self + } + pub fn new(shard: Shard, postgres_url: &str) -> Result { let config: Config = match postgres_url.parse() { Ok(config) => config, @@ -246,3 +262,31 @@ impl ForeignServer { Ok(different(existing, needed)) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fdw_address_overrides_connection() { + let url = "postgresql://graph:pw@client-endpoint:6432/graph"; + + let server = ForeignServer::new_from_raw("shard_1".to_string(), url).unwrap(); + assert_eq!( + (server.host.as_str(), server.port), + ("client-endpoint", 6432) + ); + + let server = ForeignServer::new_from_raw("shard_1".to_string(), url) + .unwrap() + .with_fdw_address(Some("10.0.0.5"), Some(5432)); + assert_eq!((server.host.as_str(), server.port), ("10.0.0.5", 5432)); + assert_eq!(server.user, "graph"); + assert_eq!(server.dbname, "graph"); + + let server = ForeignServer::new_from_raw("shard_1".to_string(), url) + .unwrap() + .with_fdw_address(Some("10.0.0.5"), None); + assert_eq!((server.host.as_str(), server.port), ("10.0.0.5", 6432)); + } +}