diff --git a/pkg/server/server_sql.go b/pkg/server/server_sql.go index f944e91421c0..005129b11b1c 100644 --- a/pkg/server/server_sql.go +++ b/pkg/server/server_sql.go @@ -12,6 +12,8 @@ package server import ( "context" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "math" "net" "os" @@ -252,7 +254,6 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) { codec = keys.MakeSQLCodec(override) } } - // Create blob service for inter-node file sharing. blobService, err := blobs.NewBlobService(cfg.Settings.ExternalIODir) if err != nil { @@ -745,6 +746,22 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) { }, nil } +// Checks if tenant exists. This function does a very superficial check to see if the system db +// has been bootstrapped for the tenant. This is not a complete check and is only sufficient +// to be used in the dev environment. +func checkTenantExists(ctx context.Context, codec keys.SQLCodec, db *kv.DB) error { + key := catalogkeys.NewDatabaseKey(systemschema.SystemDatabaseName).Key(codec) + result, err := db.Get(ctx, key) + if err != nil { + return err + } + if result.Value == nil || result.ValueInt() != keys.SystemDatabaseID { + return errors.New("System DB uninitialized, check if tenant is non existent") + } + // Tenant has been bootstrapped successfully + return nil +} + func (s *SQLServer) preStart( ctx context.Context, stopper *stop.Stopper, @@ -763,6 +780,13 @@ func (s *SQLServer) preStart( return err } } + // Confirm tenant exists prior to initialization. This is a sanity + // check for the dev environment to ensure that a tenant has been + // successfully created before attempting to initialize a SQL + // server for it. + if err := checkTenantExists(ctx, s.execCfg.Codec, s.execCfg.DB); err != nil { + return err + } s.connManager = connManager s.pgL = pgL s.execCfg.GCJobNotifier.Start(ctx)