Skip to content

Commit 902a44c

Browse files
committed
server: add TenantReadOnly flag to ExecutorConfig
Add TenantReadOnly field to SQLConfig and ExecutorConfig to track tenant-level read-only status. Modify tenant initialization to detect read-only tenants by checking mtinfopb.TenantInfoWithUsage.ReadFromTenant. Cherry-picked from ab73c65 (excluding stats-related files) Release note: None
1 parent 3bf4d17 commit 902a44c

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

pkg/base/test_server_args.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,10 @@ type TestSharedProcessTenantArgs struct {
635635
// TenantID is the ID of the tenant to be created. If not set, an ID is
636636
// assigned automatically.
637637
TenantID roachpb.TenantID
638+
// TenantReadOnly indicates if this tenant should be created as read-only
639+
// (for testing PCR reader tenants). This field is used for testing purposes
640+
// and overrides the tenant record check.
641+
TenantReadOnly bool
638642

639643
Knobs TestingKnobs
640644

pkg/server/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ type SQLConfig struct {
487487
TenantID roachpb.TenantID
488488
TenantName roachpb.TenantName
489489

490+
// TenantReadOnly indicates if this tenant is read-only (PCR reader tenant).
491+
TenantReadOnly bool
492+
490493
// If set, will to be called at server startup to obtain the tenant id and
491494
// locality.
492495
DelayedSetTenantID func(context.Context) (roachpb.TenantID, roachpb.Locality, error)
@@ -554,8 +557,9 @@ func MakeSQLConfig(
554557
tenID roachpb.TenantID, tenName roachpb.TenantName, tempStorageCfg base.TempStorageConfig,
555558
) SQLConfig {
556559
sqlCfg := SQLConfig{
557-
TenantID: tenID,
558-
TenantName: tenName,
560+
TenantID: tenID,
561+
TenantName: tenName,
562+
TenantReadOnly: false, // Default to false, will be set during tenant initialization
559563
}
560564
sqlCfg.SetDefaults(tempStorageCfg)
561565
return sqlCfg

pkg/server/server_controller_new_server.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,18 @@ func (s *topLevelServer) newTenantServer(
6464
portStartHint int,
6565
testArgs base.TestSharedProcessTenantArgs,
6666
) (onDemandServer, error) {
67-
tenantID, err := s.getTenantID(ctx, tenantNameContainer.Get())
67+
tenantID, tenantReadOnly, err := s.getTenantID(ctx, tenantNameContainer.Get())
6868
if err != nil {
6969
return nil, err
7070
}
7171

72+
// Use test override for tenant read-only status if provided.
73+
if testArgs.TenantReadOnly {
74+
tenantReadOnly = true
75+
}
76+
7277
baseCfg, sqlCfg, err := s.makeSharedProcessTenantConfig(ctx, tenantID, tenantNameContainer.Get(), portStartHint,
73-
tenantStopper, testArgs.Settings)
78+
tenantStopper, testArgs.Settings, tenantReadOnly)
7479
if err != nil {
7580
return nil, err
7681
}
@@ -100,23 +105,27 @@ var ErrInvalidTenant error = errInvalidTenantMarker{}
100105

101106
func (s *topLevelServer) getTenantID(
102107
ctx context.Context, tenantName roachpb.TenantName,
103-
) (roachpb.TenantID, error) {
108+
) (roachpb.TenantID, bool, error) {
104109
var rec *mtinfopb.TenantInfo
105110
if err := s.sqlServer.internalDB.Txn(ctx, func(ctx context.Context, txn isql.Txn) error {
106111
var err error
107112
rec, err = sql.GetTenantRecordByName(ctx, s.cfg.Settings, txn, tenantName)
108113
return err
109114
}); err != nil {
110-
return roachpb.TenantID{}, errors.Mark(err, ErrInvalidTenant)
115+
return roachpb.TenantID{}, false, errors.Mark(err, ErrInvalidTenant)
111116
}
112117

113118
tenantID, err := roachpb.MakeTenantID(rec.ID)
114119
if err != nil {
115-
return roachpb.TenantID{}, errors.Mark(
120+
return roachpb.TenantID{}, false, errors.Mark(
116121
errors.NewAssertionErrorWithWrappedErrf(err, "stored tenant ID %d does not convert to TenantID", rec.ID),
117122
ErrInvalidTenant)
118123
}
119-
return tenantID, nil
124+
125+
// Check if tenant is read-only (PCR reader tenant).
126+
readOnlyTenant := rec.ReadFromTenant != nil
127+
128+
return tenantID, readOnlyTenant, nil
120129
}
121130

122131
// newTenantServerInternal instantiates a server for the given target
@@ -151,6 +160,7 @@ func (s *topLevelServer) makeSharedProcessTenantConfig(
151160
portStartHint int,
152161
stopper *stop.Stopper,
153162
testSettings *cluster.Settings,
163+
tenantReadOnly bool,
154164
) (BaseConfig, SQLConfig, error) {
155165
// Create a configuration for the new tenant.
156166
parentCfg := s.cfg
@@ -167,7 +177,7 @@ func (s *topLevelServer) makeSharedProcessTenantConfig(
167177
}
168178

169179
baseCfg, sqlCfg, err := makeSharedProcessTenantServerConfig(ctx, tenantID, tenantName, portStartHint, parentCfg,
170-
localServerInfo, st, stopper, s.recorder)
180+
localServerInfo, st, stopper, s.recorder, tenantReadOnly)
171181
if err != nil {
172182
return BaseConfig{}, SQLConfig{}, err
173183
}
@@ -186,6 +196,7 @@ func makeSharedProcessTenantServerConfig(
186196
st *cluster.Settings,
187197
stopper *stop.Stopper,
188198
nodeMetricsRecorder *status.MetricsRecorder,
199+
tenantReadOnly bool,
189200
) (baseCfg BaseConfig, sqlCfg SQLConfig, err error) {
190201
tr := tracing.NewTracerWithOpt(ctx, tracing.WithClusterSettings(&st.SV))
191202

@@ -333,6 +344,7 @@ func makeSharedProcessTenantServerConfig(
333344
}
334345

335346
sqlCfg = MakeSQLConfig(tenantID, tenantName, tempStorageCfg)
347+
sqlCfg.TenantReadOnly = tenantReadOnly
336348
baseCfg.ExternalIODirConfig = kvServerCfg.BaseConfig.ExternalIODirConfig
337349

338350
baseCfg.ExternalIODir = kvServerCfg.BaseConfig.ExternalIODir

pkg/server/server_sql.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
10521052
RangeStatsFetcher: rangeStatsFetcher,
10531053
NodeDescs: cfg.nodeDescs,
10541054
TenantCapabilitiesReader: cfg.tenantCapabilitiesReader,
1055+
TenantReadOnly: cfg.SQLConfig.TenantReadOnly,
10551056
CidrLookup: cfg.BaseConfig.CidrLookup,
10561057
LicenseEnforcer: cfg.SQLConfig.LicenseEnforcer,
10571058
}

pkg/sql/exec_util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,9 @@ type ExecutorConfig struct {
16771677

16781678
TenantCapabilitiesReader SystemTenantOnly[tenantcapabilities.Reader]
16791679

1680+
// TenantReadOnly indicates if this tenant is read-only (PCR reader tenant).
1681+
TenantReadOnly bool
1682+
16801683
// VirtualClusterName contains the name of the virtual cluster
16811684
// (tenant).
16821685
VirtualClusterName roachpb.TenantName

0 commit comments

Comments
 (0)