|
| 1 | +// Copyright 2023 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package mtinfo |
| 12 | + |
| 13 | +import ( |
| 14 | + "github.com/cockroachdb/cockroach/pkg/multitenant/mtinfopb" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/util/protoutil" |
| 19 | + "github.com/cockroachdb/errors" |
| 20 | +) |
| 21 | + |
| 22 | +// GetTenantInfoFromSQLRow synthetizes a TenantInfo from a SQL row |
| 23 | +// extracted from system.tenants. The caller is responsible for |
| 24 | +// passing a []tree.Datum of length 2 or more (ID, info, name*, data |
| 25 | +// state*, service mode*). The values besides ID and info can be |
| 26 | +// absent or NULL; the logic knows how to handle data collected across |
| 27 | +// the v23.1 version boundary that introduced the name, data state and |
| 28 | +// service mode columns. |
| 29 | +func GetTenantInfoFromSQLRow( |
| 30 | + row tree.Datums, |
| 31 | +) (tid roachpb.TenantID, info *mtinfopb.TenantInfo, err error) { |
| 32 | + if len(row) < 2 { |
| 33 | + return tid, nil, errors.AssertionFailedf("expected at least 2 columns, got %d", len(row)) |
| 34 | + } |
| 35 | + info = &mtinfopb.TenantInfo{} |
| 36 | + |
| 37 | + idval, ok := tree.AsDInt(row[0]) |
| 38 | + if !ok { |
| 39 | + return tid, nil, errors.AssertionFailedf("tenant ID: expected int, got %T", row[0]) |
| 40 | + } |
| 41 | + tid, err = roachpb.MakeTenantID(uint64(idval)) |
| 42 | + if err != nil { |
| 43 | + return tid, nil, errors.NewAssertionErrorWithWrappedErrf(err, "%v", idval) |
| 44 | + } |
| 45 | + |
| 46 | + info.ID = tid.ToUint64() |
| 47 | + |
| 48 | + // For the benefit of pre-23.1 BACKUP/RESTORE. |
| 49 | + info.DeprecatedID = info.ID |
| 50 | + |
| 51 | + if len(row) < 2 || row[1] == tree.DNull { |
| 52 | + return tid, nil, errors.AssertionFailedf("%v: missing data in info column", tid) |
| 53 | + } |
| 54 | + ival, ok := tree.AsDBytes(row[1]) |
| 55 | + if !ok { |
| 56 | + return tid, nil, errors.AssertionFailedf("%v: info: expected bytes, got %T", tid, row[1]) |
| 57 | + } |
| 58 | + infoBytes := []byte(ival) |
| 59 | + if err := protoutil.Unmarshal(infoBytes, &info.ProtoInfo); err != nil { |
| 60 | + return tid, nil, errors.NewAssertionErrorWithWrappedErrf(err, "%v: decoding info column", tid) |
| 61 | + } |
| 62 | + |
| 63 | + // If we are loading the entry for the system tenant, inject all the |
| 64 | + // capabilities that the system tenant should have. This will allow |
| 65 | + // us to reduce our dependency on a tenant ID check to retain the |
| 66 | + // system tenant's access to all services. |
| 67 | + if tid.IsSystem() { |
| 68 | + tenantcapabilities.EnableAll(&info.ProtoInfo.Capabilities) |
| 69 | + } |
| 70 | + |
| 71 | + // Load the name if defined. |
| 72 | + if len(row) > 2 && row[2] != tree.DNull { |
| 73 | + name, ok := tree.AsDString(row[2]) |
| 74 | + if !ok { |
| 75 | + return tid, nil, errors.AssertionFailedf("%v: name: expected string, got %T", tid, row[2]) |
| 76 | + } |
| 77 | + info.Name = roachpb.TenantName(name) |
| 78 | + } |
| 79 | + |
| 80 | + // Load the data state column if defined. |
| 81 | + // Compute a suitable default value, from the pre-v23.1 info struct. |
| 82 | + switch info.ProtoInfo.DeprecatedDataState { |
| 83 | + case mtinfopb.ProtoInfo_READY: |
| 84 | + info.DataState = mtinfopb.DataStateReady |
| 85 | + case mtinfopb.ProtoInfo_ADD: |
| 86 | + info.DataState = mtinfopb.DataStateAdd |
| 87 | + case mtinfopb.ProtoInfo_DROP: |
| 88 | + info.DataState = mtinfopb.DataStateDrop |
| 89 | + default: |
| 90 | + return tid, nil, errors.AssertionFailedf("%v: unhandled: %d", tid, info.ProtoInfo.DeprecatedDataState) |
| 91 | + } |
| 92 | + if len(row) > 3 && row[3] != tree.DNull { |
| 93 | + val, ok := tree.AsDInt(row[3]) |
| 94 | + if !ok { |
| 95 | + return tid, nil, errors.AssertionFailedf("%v: data state: expected int, got %T", tid, row[3]) |
| 96 | + } |
| 97 | + if val < 0 || mtinfopb.TenantDataState(val) > mtinfopb.MaxDataState { |
| 98 | + return tid, nil, errors.AssertionFailedf("%v: invalid data state: %d", tid, val) |
| 99 | + } else { |
| 100 | + info.DataState = mtinfopb.TenantDataState(val) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Load the service mode if defined. |
| 105 | + if info.DataState == mtinfopb.DataStateReady { |
| 106 | + // Suitable default for records created for CC Serverless pre-v23.1. |
| 107 | + info.ServiceMode = mtinfopb.ServiceModeExternal |
| 108 | + } else { |
| 109 | + // Suitable default for in-progress records created for CC Serverless pre-v23.1. |
| 110 | + info.ServiceMode = mtinfopb.ServiceModeNone |
| 111 | + } |
| 112 | + if len(row) > 4 && row[4] != tree.DNull { |
| 113 | + val, ok := tree.AsDInt(row[4]) |
| 114 | + if !ok { |
| 115 | + return tid, nil, errors.AssertionFailedf("%v: service mode: expected int, got %T", tid, row[4]) |
| 116 | + } |
| 117 | + if val < 0 || mtinfopb.TenantServiceMode(val) > mtinfopb.MaxServiceMode { |
| 118 | + return tid, nil, errors.AssertionFailedf("%v: invalid service mode: %d", tid, val) |
| 119 | + } else { |
| 120 | + info.ServiceMode = mtinfopb.TenantServiceMode(val) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return tid, info, nil |
| 125 | +} |
0 commit comments