Skip to content
Open
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
21 changes: 21 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
42 changes: 42 additions & 0 deletions node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// The port that goes with `fdw_host`. Defaults to the port in
/// `connection`
#[serde(default)]
pub fdw_port: Option<u16>,
#[serde(default)]
pub replicas: BTreeMap<String, Replica>,
}
Expand All @@ -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));
Expand Down Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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::<Shard>(
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::<Shard>(
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(
Expand Down
6 changes: 5 additions & 1 deletion node/src/store_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<Vec<_>, _>>()
.expect("connection url's contain enough detail");
let servers = Arc::new(servers);
Expand Down
44 changes: 44 additions & 0 deletions store/postgres/src/pool/foreign_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>) -> 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<Self, anyhow::Error> {
let config: Config = match postgres_url.parse() {
Ok(config) => config,
Expand Down Expand Up @@ -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));
}
}
Loading