Skip to content

Commit

Permalink
sql,ua: poc of copying system.zones table using kv rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdhama committed Nov 6, 2024
1 parent 23b754d commit 0c2af6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/sql/catalog/bootstrap/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ func addZoneConfigKVsToSchema(
defaultZoneConfig *zonepb.ZoneConfig,
defaultSystemZoneConfig *zonepb.ZoneConfig,
) {
kvs := InitialZoneConfigKVs(target.codec, defaultZoneConfig, defaultSystemZoneConfig)
var kvs []roachpb.KeyValue
if target.codec.TenantID != roachpb.TenantTwo {
kvs = InitialZoneConfigKVs(target.codec, defaultZoneConfig, defaultSystemZoneConfig)
}
target.otherKV = append(target.otherKV, kvs...)
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/sql/tenant_creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ func BootstrapTenant(
return tid, err
}

{
copiedKVs, err := CopyZoneConfigKVs(ctx, txn, codec)
if err != nil {
return tid, err
}
kvs = append(kvs, copiedKVs...)
}

{
// Populate the version setting for the tenant. This will allow the tenant
// to know what migrations need to be run in the future. The choice to use
Expand Down Expand Up @@ -242,6 +250,43 @@ func BootstrapTenant(
return tid, nil
}

func CopyZoneConfigKVs(
ctx context.Context, txn *kv.Txn, targetCodec keys.SQLCodec,
) ([]roachpb.KeyValue, error) {
sourceCodec := keys.SystemSQLCodec
// get all keys for the table

zoneSpan := sourceCodec.TableSpan(keys.ZonesTableID)
batch := txn.NewBatch()
batch.Scan(zoneSpan.Key, zoneSpan.EndKey)

if err := txn.Run(ctx, batch); err != nil {
return nil, err
}

if len(batch.Results) != 1 {
return nil, errors.AssertionFailedf("expected exactly on batch result found %d: %+v", len(batch.Results), batch.Results[1])
}

if err := batch.Results[0].Err; err != nil {
return nil, err
}
rows := batch.Results[0].Rows

// rewrite keys
targetTenantPrefix := targetCodec.TenantPrefix()
ret := make([]roachpb.KeyValue, 0, len(rows))
for _, row := range rows {
v := roachpb.KeyValue{
Key: append(targetTenantPrefix, row.Key...),
Value: *row.Value,
}
v.Value.ClearChecksum()
ret = append(ret, v)
}
return ret, nil
}

// CreateTenantRecord creates a tenant in system.tenants and installs an initial
// span config (in system.span_configurations) for it. It also initializes the
// usage data in system.tenant_usage if info.Usage is set.
Expand Down

0 comments on commit 0c2af6e

Please sign in to comment.