github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/roachpb/tenant.go (about) 1 // Copyright 2020 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 roachpb 12 13 import ( 14 "math" 15 "strconv" 16 ) 17 18 // A TenantID is a unique ID associated with a tenant in a multi-tenant cluster. 19 // Each tenant is granted exclusive access to a portion of the keyspace and a 20 // collection of SQL tables in that keyspace which comprise a "logical" cluster. 21 // 22 // The type is intentionally opaque to require deliberate use. 23 type TenantID struct{ id uint64 } 24 25 // SystemTenantID is the ID associated with the system's internal tenant in a 26 // multi-tenant cluster and the only tenant in a single-tenant cluster. 27 // 28 // The system tenant differs from all other tenants in four important ways: 29 // 1. the system tenant's keyspace is not prefixed with a tenant specifier. 30 // 2. the system tenant is created by default during cluster initialization. 31 // 3. the system tenant is always present and can never be destroyed. 32 // 4. the system tenant has the ability to create and destroy other tenants. 33 var SystemTenantID = MakeTenantID(1) 34 35 // MinTenantID is the minimum ID of a (non-system) tenant in a multi-tenant 36 // cluster. 37 var MinTenantID = MakeTenantID(2) 38 39 // MaxTenantID is the maximum ID of a (non-system) tenant in a multi-tenant 40 // cluster. 41 var MaxTenantID = MakeTenantID(math.MaxUint64) 42 43 // MakeTenantID constructs a new TenantID from the provided uint64. 44 func MakeTenantID(id uint64) TenantID { 45 checkValid(id) 46 return TenantID{id} 47 } 48 49 // ToUint64 returns the TenantID as a uint64. 50 func (t TenantID) ToUint64() uint64 { 51 checkValid(t.id) 52 return t.id 53 } 54 55 // String implements the fmt.Stringer interface. 56 func (t TenantID) String() string { 57 switch t { 58 case TenantID{}: 59 return "invalid" 60 case SystemTenantID: 61 return "system" 62 default: 63 return strconv.FormatUint(t.id, 10) 64 } 65 } 66 67 // Protects against zero value. 68 func checkValid(id uint64) { 69 if id == 0 { 70 panic("invalid tenant ID 0") 71 } 72 } 73 74 // IsSystemTenantID returns whether the provided ID corresponds to that of the 75 // system tenant. 76 func IsSystemTenantID(id uint64) bool { 77 return id == SystemTenantID.ToUint64() 78 }