From b6f5c395cb97d8f4edde07c3dd879e0503f11de8 Mon Sep 17 00:00:00 2001 From: Joonas Koivunen Date: Thu, 27 Jul 2023 14:07:42 +0300 Subject: [PATCH] neon_local: add check against duplicate primaries --- control_plane/src/endpoint.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/control_plane/src/endpoint.rs b/control_plane/src/endpoint.rs index 6df6e47f29..ff79a83ba9 100644 --- a/control_plane/src/endpoint.rs +++ b/control_plane/src/endpoint.rs @@ -128,6 +128,20 @@ impl ComputeControlPlane { ) -> Result> { let pg_port = pg_port.unwrap_or_else(|| self.get_port()); let http_port = http_port.unwrap_or_else(|| self.get_port() + 1); + + if matches!(mode, ComputeMode::Primary) { + // this check is not complete, as you could have a concurrent attempt at + // creating another primary, both reading the state before checking it here, + // but it's better than nothing. + let mut duplicates = self.endpoints.iter().filter(|(_k, v)| { + v.tenant_id == tenant_id && v.timeline_id == timeline_id && v.mode == mode + }); + + if let Some((key, _)) = duplicates.next() { + bail!("attempting to create a duplicate primary endpoint on tenant {tenant_id}, timeline {timeline_id}: endpoint {key:?} exists already. please don't do this, it is not supported."); + } + } + let ep = Arc::new(Endpoint { endpoint_id: endpoint_id.to_owned(), pg_address: SocketAddr::new("127.0.0.1".parse().unwrap(), pg_port),