diff --git a/pageserver/src/page_cache.rs b/pageserver/src/page_cache.rs index 4e49cbef3e..1467afeefc 100644 --- a/pageserver/src/page_cache.rs +++ b/pageserver/src/page_cache.rs @@ -1,12 +1,13 @@ //! This module acts as a switchboard to access different repositories managed by this //! page server. +use crate::branches; use crate::object_repository::ObjectRepository; use crate::repository::Repository; use crate::rocksdb_storage::RocksObjectStore; use crate::walredo::PostgresRedoManager; use crate::{PageServerConf, ZTenantId}; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, bail, Result}; use lazy_static::lazy_static; use log::info; use std::collections::HashMap; @@ -38,6 +39,24 @@ pub fn init(conf: &'static PageServerConf) { } } +pub fn create_repository_for_tenant( + conf: &'static PageServerConf, + tenantid: ZTenantId, +) -> Result<()> { + let mut m = REPOSITORY.lock().unwrap(); + + // First check that the tenant doesn't exist already + if m.get(&tenantid).is_some() { + bail!("tenant {} already exists", tenantid); + } + let wal_redo_manager = Arc::new(PostgresRedoManager::new(conf, tenantid)); + let repo = branches::create_repo(conf, tenantid, wal_redo_manager)?; + + m.insert(tenantid, Arc::new(repo)); + + Ok(()) +} + pub fn insert_repository_for_tenant(tenantid: ZTenantId, repo: Arc) { let o = &mut REPOSITORY.lock().unwrap(); o.insert(tenantid, repo); diff --git a/pageserver/src/page_service.rs b/pageserver/src/page_service.rs index 434cfffbcd..2855abbd44 100644 --- a/pageserver/src/page_service.rs +++ b/pageserver/src/page_service.rs @@ -17,7 +17,6 @@ use regex::Regex; use std::io::Write; use std::net::TcpListener; use std::str::FromStr; -use std::sync::Arc; use std::thread; use std::{io, net::TcpStream}; use zenith_utils::postgres_backend::PostgresBackend; @@ -33,7 +32,6 @@ use crate::object_key::ObjectTag; use crate::page_cache; use crate::repository::{BufferTag, Modification, RelTag}; use crate::walreceiver; -use crate::walredo::PostgresRedoManager; use crate::PageServerConf; use crate::ZTenantId; use crate::ZTimelineId; @@ -508,9 +506,8 @@ impl postgres_backend::Handler for PageServerHandler { let caps = re.captures(&query_string).ok_or_else(err)?; let tenantid = ZTenantId::from_str(caps.get(1).unwrap().as_str())?; - let wal_redo_manager = Arc::new(PostgresRedoManager::new(self.conf, tenantid)); - let repo = branches::create_repo(self.conf, tenantid, wal_redo_manager)?; - page_cache::insert_repository_for_tenant(tenantid, Arc::new(repo)); + + page_cache::create_repository_for_tenant(&self.conf, tenantid)?; pgb.write_message_noflush(&SINGLE_COL_ROWDESC)? .write_message_noflush(&BeMessage::CommandComplete(b"SELECT 1"))?; diff --git a/test_runner/batch_others/test_pageserver_api.py b/test_runner/batch_others/test_pageserver_api.py index 4bc0c36a08..3410eea90a 100644 --- a/test_runner/batch_others/test_pageserver_api.py +++ b/test_runner/batch_others/test_pageserver_api.py @@ -62,7 +62,7 @@ def test_tenant_list(pageserver: ZenithPageserver, zenith_cli): cur = conn.cursor() # check same tenant cannot be created twice - with pytest.raises(psycopg2.DatabaseError, match=f'repo for {pageserver.initial_tenant} already exists'): + with pytest.raises(psycopg2.DatabaseError, match=f'tenant {pageserver.initial_tenant} already exists'): cur.execute(f'tenant_create {pageserver.initial_tenant}') # create one more tenant