From 14b0acbda7cfebb1c1bec12411c8a8d8b4ddbd5f Mon Sep 17 00:00:00 2001 From: John Spray Date: Tue, 12 Dec 2023 16:28:58 +0000 Subject: [PATCH] neon_local: improved timeline creation and 'branch' --- control_plane/src/attachment_service.rs | 4 +- control_plane/src/bin/attachment_service.rs | 29 ++++++++++---- control_plane/src/bin/neon_local.rs | 42 +++++++++++---------- control_plane/src/pageserver.rs | 4 +- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/control_plane/src/attachment_service.rs b/control_plane/src/attachment_service.rs index 0ce8f6194b..331bb00e2c 100644 --- a/control_plane/src/attachment_service.rs +++ b/control_plane/src/attachment_service.rs @@ -3,7 +3,7 @@ use anyhow::anyhow; use camino::Utf8PathBuf; use hyper::{Method, StatusCode}; use pageserver_api::{ - models::{TenantCreateRequest, TenantShardSplitRequest, TimelineCreateRequest}, + models::{TenantCreateRequest, TenantShardSplitRequest, TimelineCreateRequest, TimelineInfo}, shard::TenantShardId, }; use postgres_connection::parse_host_port; @@ -271,7 +271,7 @@ impl AttachmentService { &self, tenant_id: TenantId, req: TimelineCreateRequest, - ) -> anyhow::Result<()> { + ) -> anyhow::Result { self.dispatch( Method::POST, format!("tenant/{tenant_id}/timeline"), diff --git a/control_plane/src/bin/attachment_service.rs b/control_plane/src/bin/attachment_service.rs index 4a9b461a9e..778b2537a6 100644 --- a/control_plane/src/bin/attachment_service.rs +++ b/control_plane/src/bin/attachment_service.rs @@ -11,7 +11,7 @@ use hyper::{Method, StatusCode}; use pageserver_api::models::{ LocationConfig, LocationConfigMode, TenantConfig, TenantCreateRequest, TenantLocationConfigRequest, TenantShardSplitRequest, TenantShardSplitResponse, - TimelineCreateRequest, + TimelineCreateRequest, TimelineInfo, }; use pageserver_api::shard::{ShardCount, ShardIdentity, ShardNumber, TenantShardId}; use reqwest::Client; @@ -225,7 +225,7 @@ impl TenantState { &self, node: &NodeState, req: &TimelineCreateRequest, - ) -> anyhow::Result<()> { + ) -> anyhow::Result { let client = Client::new(); let response = client .request( @@ -239,9 +239,9 @@ impl TenantState { .json(req) .send() .await?; - response.error_for_status()?; + response.error_for_status_ref()?; - Ok(()) + Ok(response.json().await?) } fn schedule(&mut self, scheduler: &mut Scheduler) -> Result<(), ScheduleError> { @@ -620,7 +620,7 @@ async fn handle_tenant_create(mut req: Request) -> Result, async fn handle_tenant_timeline_create(mut req: Request) -> Result, ApiError> { let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?; - let create_req = json_request::(&mut req).await?; + let mut create_req = json_request::(&mut req).await?; let state = get_state(&req).inner.clone(); let mut locked = state.write().await; @@ -637,6 +637,8 @@ async fn handle_tenant_timeline_create(mut req: Request) -> Result) -> Result) -> Result, ApiError> { diff --git a/control_plane/src/bin/neon_local.rs b/control_plane/src/bin/neon_local.rs index f1ebd0922f..f645280687 100644 --- a/control_plane/src/bin/neon_local.rs +++ b/control_plane/src/bin/neon_local.rs @@ -603,18 +603,19 @@ async fn handle_timeline(timeline_match: &ArgMatches, env: &mut local_env::Local .context("Failed to parse postgres version from the argument string")?; let new_timeline_id_opt = parse_timeline_id(create_match)?; + let new_timeline_id = new_timeline_id_opt.unwrap_or(TimelineId::generate()); - let timeline_info = pageserver - .timeline_create( - tenant_id, - new_timeline_id_opt, - None, - None, - Some(pg_version), - None, - ) + let attachment_service = AttachmentService::from_env(env); + let create_req = TimelineCreateRequest { + new_timeline_id: new_timeline_id, + ancestor_timeline_id: None, + existing_initdb_timeline_id: None, + ancestor_start_lsn: None, + pg_version: Some(pg_version), + }; + let timeline_info = attachment_service + .tenant_timeline_create(tenant_id, create_req) .await?; - let new_timeline_id = timeline_info.timeline_id; let last_record_lsn = timeline_info.last_record_lsn; env.register_branch_mapping(new_branch_name.to_string(), tenant_id, new_timeline_id)?; @@ -695,17 +696,18 @@ async fn handle_timeline(timeline_match: &ArgMatches, env: &mut local_env::Local .map(|lsn_str| Lsn::from_str(lsn_str)) .transpose() .context("Failed to parse ancestor start Lsn from the request")?; - let timeline_info = pageserver - .timeline_create( - tenant_id, - None, - start_lsn, - Some(ancestor_timeline_id), - None, - None, - ) + let new_timeline_id = TimelineId::generate(); + let attachment_service = AttachmentService::from_env(env); + let create_req = TimelineCreateRequest { + new_timeline_id: new_timeline_id, + ancestor_timeline_id: Some(ancestor_timeline_id), + existing_initdb_timeline_id: None, + ancestor_start_lsn: start_lsn, + pg_version: None, + }; + let timeline_info = attachment_service + .tenant_timeline_create(tenant_id, create_req) .await?; - let new_timeline_id = timeline_info.timeline_id; let last_record_lsn = timeline_info.last_record_lsn; diff --git a/control_plane/src/pageserver.rs b/control_plane/src/pageserver.rs index 4e5c8643a5..d43d39bde9 100644 --- a/control_plane/src/pageserver.rs +++ b/control_plane/src/pageserver.rs @@ -498,14 +498,12 @@ impl PageServerNode { pub async fn timeline_create( &self, tenant_id: TenantId, - new_timeline_id: Option, + new_timeline_id: TimelineId, ancestor_start_lsn: Option, ancestor_timeline_id: Option, pg_version: Option, existing_initdb_timeline_id: Option, ) -> anyhow::Result { - // If timeline ID was not specified, generate one - let new_timeline_id = new_timeline_id.unwrap_or(TimelineId::generate()); let req = models::TimelineCreateRequest { new_timeline_id, ancestor_start_lsn,