github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/keys/constants.go (about)

     1  // Copyright 2015 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 keys
    12  
    13  import (
    14  	"math"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    17  )
    18  
    19  // For a high-level overview of the keyspace layout, see the package comment in
    20  // doc.go.
    21  
    22  // These constants are single bytes for performance. They allow single-byte
    23  // comparisons which are considerably faster than bytes.HasPrefix.
    24  const (
    25  	localPrefixByte  = '\x01'
    26  	localMaxByte     = '\x02'
    27  	meta1PrefixByte  = localMaxByte
    28  	meta2PrefixByte  = '\x03'
    29  	metaMaxByte      = '\x04'
    30  	systemPrefixByte = metaMaxByte
    31  	systemMaxByte    = '\x05'
    32  	tenantPrefixByte = '\xfe'
    33  )
    34  
    35  // Constants for system-reserved keys in the KV map.
    36  //
    37  // Note: Preserve group-wise ordering when adding new constants.
    38  // Note: Update `keymap` in doc.go when adding new constants.
    39  var (
    40  	// MinKey is a minimum key value which sorts before all other keys.
    41  	MinKey = roachpb.KeyMin
    42  	// MaxKey is the infinity marker which is larger than any other key.
    43  	MaxKey = roachpb.KeyMax
    44  
    45  	// localPrefix is the prefix for all local keys.
    46  	localPrefix = roachpb.Key{localPrefixByte}
    47  	// LocalMax is the end of the local key range. It is itself a global
    48  	// key.
    49  	LocalMax = roachpb.Key{localMaxByte}
    50  
    51  	// localSuffixLength specifies the length in bytes of all local
    52  	// key suffixes.
    53  	localSuffixLength = 4
    54  
    55  	// There are four types of local key data enumerated below: replicated
    56  	// range-ID, unreplicated range-ID, range local, and store-local keys.
    57  
    58  	// 1. Replicated Range-ID keys
    59  	//
    60  	// LocalRangeIDPrefix is the prefix identifying per-range data
    61  	// indexed by Range ID. The Range ID is appended to this prefix,
    62  	// encoded using EncodeUvarint. The specific sort of per-range
    63  	// metadata is identified by one of the suffixes listed below, along
    64  	// with potentially additional encoded key info, for instance in the
    65  	// case of AbortSpan entry.
    66  	//
    67  	// NOTE: LocalRangeIDPrefix must be kept in sync with the value
    68  	// in storage/engine/rocksdb/db.cc.
    69  	LocalRangeIDPrefix = roachpb.RKey(makeKey(localPrefix, roachpb.Key("i")))
    70  	// LocalRangeIDReplicatedInfix is the post-Range ID specifier for all Raft
    71  	// replicated per-range data. By appending this after the Range ID, these
    72  	// keys will be sorted directly before the local unreplicated keys for the
    73  	// same Range ID, so they can be manipulated either together or individually
    74  	// in a single scan.
    75  	LocalRangeIDReplicatedInfix = []byte("r")
    76  	// LocalAbortSpanSuffix is the suffix for AbortSpan entries. The
    77  	// AbortSpan protects a transaction from re-reading its own intents
    78  	// after it's been aborted.
    79  	LocalAbortSpanSuffix = []byte("abc-")
    80  	// localRangeFrozenStatusSuffix is DEPRECATED and remains to prevent reuse.
    81  	localRangeFrozenStatusSuffix = []byte("fzn-")
    82  	// LocalRangeLastGCSuffix is the suffix for the last GC.
    83  	LocalRangeLastGCSuffix = []byte("lgc-")
    84  	// LocalRangeAppliedStateSuffix is the suffix for the range applied state
    85  	// key.
    86  	LocalRangeAppliedStateSuffix = []byte("rask")
    87  	// LocalRaftAppliedIndexLegacySuffix is the suffix for the raft applied index.
    88  	LocalRaftAppliedIndexLegacySuffix = []byte("rfta")
    89  	// LocalRaftTruncatedStateLegacySuffix is the suffix for the legacy
    90  	// RaftTruncatedState. See VersionUnreplicatedRaftTruncatedState.
    91  	// Note: This suffix is also used for unreplicated Range-ID keys.
    92  	LocalRaftTruncatedStateLegacySuffix = []byte("rftt")
    93  	// LocalRangeLeaseSuffix is the suffix for a range lease.
    94  	LocalRangeLeaseSuffix = []byte("rll-")
    95  	// LocalLeaseAppliedIndexLegacySuffix is the suffix for the applied lease
    96  	// index.
    97  	LocalLeaseAppliedIndexLegacySuffix = []byte("rlla")
    98  	// LocalRangeStatsLegacySuffix is the suffix for range statistics.
    99  	LocalRangeStatsLegacySuffix = []byte("stat")
   100  	// localTxnSpanGCThresholdSuffix is DEPRECATED and remains to prevent reuse.
   101  	localTxnSpanGCThresholdSuffix = []byte("tst-")
   102  
   103  	// 2. Unreplicated Range-ID keys
   104  	//
   105  	// localRangeIDUnreplicatedInfix is the post-Range ID specifier for all
   106  	// per-range data that is not fully Raft replicated. By appending this
   107  	// after the Range ID, these keys will be sorted directly after the local
   108  	// replicated keys for the same Range ID, so they can be manipulated either
   109  	// together or individually in a single scan.
   110  	localRangeIDUnreplicatedInfix = []byte("u")
   111  	// LocalRangeTombstoneSuffix is the suffix for the range tombstone.
   112  	//
   113  	// NB: This suffix was originally named LocalRaftTombstoneSuffix, which is
   114  	// why it starts off with "rft" as opposed to "rl".
   115  	LocalRangeTombstoneSuffix = []byte("rftb")
   116  	// LocalRaftHardStateSuffix is the Suffix for the raft HardState.
   117  	LocalRaftHardStateSuffix = []byte("rfth")
   118  	// localRaftLastIndexSuffix is DEPRECATED and remains to prevent reuse.
   119  	localRaftLastIndexSuffix = []byte("rfti")
   120  	// LocalRaftLogSuffix is the suffix for the raft log.
   121  	LocalRaftLogSuffix = []byte("rftl")
   122  	// LocalRangeLastReplicaGCTimestampSuffix is the suffix for a range's last
   123  	// replica GC timestamp (for GC of old replicas).
   124  	LocalRangeLastReplicaGCTimestampSuffix = []byte("rlrt")
   125  	// localRangeLastVerificationTimestampSuffix is DEPRECATED and remains to
   126  	// prevent reuse.
   127  	localRangeLastVerificationTimestampSuffix = []byte("rlvt")
   128  
   129  	// 3. Range local keys
   130  	//
   131  	// LocalRangePrefix is the prefix identifying per-range data indexed
   132  	// by range key (either start key, or some key in the range). The
   133  	// key is appended to this prefix, encoded using EncodeBytes. The
   134  	// specific sort of per-range metadata is identified by one of the
   135  	// suffixes listed below, along with potentially additional encoded
   136  	// key info, such as the txn ID in the case of a transaction record.
   137  	//
   138  	// NOTE: LocalRangePrefix must be kept in sync with the value in
   139  	// storage/engine/rocksdb/db.cc.
   140  	LocalRangePrefix = roachpb.Key(makeKey(localPrefix, roachpb.RKey("k")))
   141  	LocalRangeMax    = LocalRangePrefix.PrefixEnd()
   142  	// LocalQueueLastProcessedSuffix is the suffix for replica queue state keys.
   143  	LocalQueueLastProcessedSuffix = roachpb.RKey("qlpt")
   144  	// LocalRangeDescriptorJointSuffix is the suffix for keys storing
   145  	// range descriptors. The value is a struct of type RangeDescriptor.
   146  	//
   147  	// TODO(tbg): decide what to actually store here. This is still unused.
   148  	LocalRangeDescriptorJointSuffix = roachpb.RKey("rdjt")
   149  	// LocalRangeDescriptorSuffix is the suffix for keys storing
   150  	// range descriptors. The value is a struct of type RangeDescriptor.
   151  	LocalRangeDescriptorSuffix = roachpb.RKey("rdsc")
   152  	// LocalTransactionSuffix specifies the key suffix for
   153  	// transaction records. The additional detail is the transaction id.
   154  	// NOTE: if this value changes, it must be updated in C++
   155  	// (storage/engine/rocksdb/db.cc).
   156  	LocalTransactionSuffix = roachpb.RKey("txn-")
   157  
   158  	// 4. Store local keys
   159  	//
   160  	// localStorePrefix is the prefix identifying per-store data.
   161  	localStorePrefix = makeKey(localPrefix, roachpb.Key("s"))
   162  	// localStoreSuggestedCompactionSuffix stores suggested compactions to
   163  	// be aggregated and processed on the store.
   164  	localStoreSuggestedCompactionSuffix = []byte("comp")
   165  	// localStoreClusterVersionSuffix stores the cluster-wide version
   166  	// information for this store, updated any time the operator
   167  	// updates the minimum cluster version.
   168  	localStoreClusterVersionSuffix = []byte("cver")
   169  	// localStoreGossipSuffix stores gossip bootstrap metadata for this
   170  	// store, updated any time new gossip hosts are encountered.
   171  	localStoreGossipSuffix = []byte("goss")
   172  	// localStoreHLCUpperBoundSuffix stores an upper bound to the wall time used by
   173  	// the HLC.
   174  	localStoreHLCUpperBoundSuffix = []byte("hlcu")
   175  	// localStoreIdentSuffix stores an immutable identifier for this
   176  	// store, created when the store is first bootstrapped.
   177  	localStoreIdentSuffix = []byte("iden")
   178  	// localStoreLastUpSuffix stores the last timestamp that a store's node
   179  	// acknowledged that it was still running. This value will be regularly
   180  	// refreshed on all stores for a running node; the intention of this value
   181  	// is to allow a restarting node to discover approximately how long it has
   182  	// been down without needing to retrieve liveness records from the cluster.
   183  	localStoreLastUpSuffix = []byte("uptm")
   184  	// localRemovedLeakedRaftEntriesSuffix is DEPRECATED and remains to prevent
   185  	// reuse.
   186  	localRemovedLeakedRaftEntriesSuffix = []byte("dlre")
   187  	// LocalStoreSuggestedCompactionsMin is the start of the span of
   188  	// possible suggested compaction keys for a store.
   189  	LocalStoreSuggestedCompactionsMin = MakeStoreKey(localStoreSuggestedCompactionSuffix, nil)
   190  	// LocalStoreSuggestedCompactionsMax is the end of the span of
   191  	// possible suggested compaction keys for a store.
   192  	LocalStoreSuggestedCompactionsMax = LocalStoreSuggestedCompactionsMin.PrefixEnd()
   193  
   194  	// The global keyspace includes the meta{1,2}, system, system tenant SQL
   195  	// keys, and non-system tenant SQL keys.
   196  
   197  	// 1. Meta keys
   198  	//
   199  	// MetaMin is the start of the range of addressing keys.
   200  	MetaMin = Meta1Prefix
   201  	// MetaMax is the end of the range of addressing keys.
   202  	MetaMax = roachpb.Key{metaMaxByte}
   203  	// Meta1Prefix is the first level of key addressing. It is selected such that
   204  	// all range addressing records sort before any system tables which they
   205  	// might describe. The value is a RangeDescriptor struct.
   206  	Meta1Prefix = roachpb.Key{meta1PrefixByte}
   207  	// Meta1KeyMax is the end of the range of the first level of key addressing.
   208  	// The value is a RangeDescriptor struct.
   209  	Meta1KeyMax = roachpb.Key(makeKey(Meta1Prefix, roachpb.RKeyMax))
   210  	// Meta2Prefix is the second level of key addressing. The value is a
   211  	// RangeDescriptor struct.
   212  	Meta2Prefix = roachpb.Key{meta2PrefixByte}
   213  	// Meta2KeyMax is the end of the range of the second level of key addressing.
   214  	// The value is a RangeDescriptor struct.
   215  	Meta2KeyMax = roachpb.Key(makeKey(Meta2Prefix, roachpb.RKeyMax))
   216  
   217  	// 2. System keys
   218  	//
   219  	// SystemPrefix indicates the beginning of the key range for
   220  	// global, system data which are replicated across the cluster.
   221  	SystemPrefix = roachpb.Key{systemPrefixByte}
   222  	SystemMax    = roachpb.Key{systemMaxByte}
   223  	// NodeLivenessPrefix specifies the key prefix for the node liveness
   224  	// table.  Note that this should sort before the rest of the system
   225  	// keyspace in order to limit the number of ranges which must use
   226  	// expiration-based range leases instead of the more efficient
   227  	// node-liveness epoch-based range leases (see
   228  	// https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20160210_range_leases.md)
   229  	NodeLivenessPrefix = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("\x00liveness-")))
   230  	// NodeLivenessKeyMax is the maximum value for any node liveness key.
   231  	NodeLivenessKeyMax = NodeLivenessPrefix.PrefixEnd()
   232  	//
   233  	// BootstrapVersion is the key at which clusters bootstrapped with a version
   234  	// > 1.0 persist the version at which they were bootstrapped.
   235  	BootstrapVersionKey = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("bootstrap-version")))
   236  	//
   237  	// descIDGenerator is the global descriptor ID generator sequence used for
   238  	// table and namespace IDs for the system tenant. All other tenants use a
   239  	// SQL sequence for this purpose. See sqlEncoder.DescIDSequenceKey.
   240  	descIDGenerator = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("desc-idgen")))
   241  	// NodeIDGenerator is the global node ID generator sequence.
   242  	NodeIDGenerator = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("node-idgen")))
   243  	// RangeIDGenerator is the global range ID generator sequence.
   244  	RangeIDGenerator = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("range-idgen")))
   245  	// StoreIDGenerator is the global store ID generator sequence.
   246  	StoreIDGenerator = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("store-idgen")))
   247  	//
   248  	// StatusPrefix specifies the key prefix to store all status details.
   249  	StatusPrefix = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("status-")))
   250  	// StatusNodePrefix stores all status info for nodes.
   251  	StatusNodePrefix = roachpb.Key(makeKey(StatusPrefix, roachpb.RKey("node-")))
   252  	//
   253  	// MigrationPrefix specifies the key prefix to store all migration details.
   254  	MigrationPrefix = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("system-version/")))
   255  	// MigrationLease is the key that nodes must take a lease on in order to run
   256  	// system migrations on the cluster.
   257  	MigrationLease = roachpb.Key(makeKey(MigrationPrefix, roachpb.RKey("lease")))
   258  	//
   259  	// TimeseriesPrefix is the key prefix for all timeseries data.
   260  	TimeseriesPrefix = roachpb.Key(makeKey(SystemPrefix, roachpb.RKey("tsd")))
   261  	// TimeseriesKeyMax is the maximum value for any timeseries data.
   262  	TimeseriesKeyMax = TimeseriesPrefix.PrefixEnd()
   263  
   264  	// 3. System tenant SQL keys
   265  	//
   266  	// TODO(nvanbenschoten): Figure out what to do with all of these. At a
   267  	// minimum, prefix them all with "System".
   268  	//
   269  	// TableDataMin is the start of the range of table data keys.
   270  	TableDataMin = SystemSQLCodec.TablePrefix(0)
   271  	// TableDataMin is the end of the range of table data keys.
   272  	TableDataMax = SystemSQLCodec.TablePrefix(math.MaxUint32).PrefixEnd()
   273  	//
   274  	// SystemConfigSplitKey is the key to split at immediately prior to the
   275  	// system config span. NB: Split keys need to be valid column keys.
   276  	// TODO(bdarnell): this should be either roachpb.Key or RKey, not []byte.
   277  	SystemConfigSplitKey = []byte(TableDataMin)
   278  	// SystemConfigTableDataMax is the end key of system config span.
   279  	SystemConfigTableDataMax = SystemSQLCodec.TablePrefix(MaxSystemConfigDescID + 1)
   280  	//
   281  	// NamespaceTableMin is the start key of system.namespace, which is a system
   282  	// table that does not reside in the same range as other system tables.
   283  	NamespaceTableMin = SystemSQLCodec.TablePrefix(NamespaceTableID)
   284  	// NamespaceTableMax is the end key of system.namespace.
   285  	NamespaceTableMax = SystemSQLCodec.TablePrefix(NamespaceTableID + 1)
   286  	//
   287  	// UserTableDataMin is the start key of user structured data.
   288  	UserTableDataMin = SystemSQLCodec.TablePrefix(MinUserDescID)
   289  
   290  	// 4. Non-system tenant SQL keys
   291  	//
   292  	// TenantPrefix is the prefix for all non-system tenant keys.
   293  	TenantPrefix       = roachpb.Key{tenantPrefixByte}
   294  	TenantTableDataMin = MakeTenantPrefix(roachpb.MinTenantID)
   295  	TenantTableDataMax = MakeTenantPrefix(roachpb.MaxTenantID).PrefixEnd()
   296  )
   297  
   298  // Various IDs used by the structured data layer.
   299  // NOTE: these must not change during the lifetime of a cluster.
   300  const (
   301  	// MaxSystemConfigDescID is the maximum system descriptor ID that will be
   302  	// gossiped as part of the SystemConfig. Be careful adding new descriptors to
   303  	// this ID range.
   304  	MaxSystemConfigDescID = 10
   305  
   306  	// MaxReservedDescID is the maximum value of reserved descriptor
   307  	// IDs. Reserved IDs are used by namespaces and tables used internally by
   308  	// cockroach.
   309  	MaxReservedDescID = 49
   310  
   311  	// MinUserDescID is the first descriptor ID available for user
   312  	// structured data.
   313  	MinUserDescID = MaxReservedDescID + 1
   314  
   315  	// MinNonPredefinedUserDescID is the first descriptor ID used by
   316  	// user-level objects that are not created automatically on empty
   317  	// clusters (default databases).
   318  	MinNonPredefinedUserDescID = MinUserDescID + 2
   319  
   320  	// RootNamespaceID is the ID of the root namespace.
   321  	RootNamespaceID = 0
   322  
   323  	// SystemDatabaseID and following are the database/table IDs for objects
   324  	// in the system span.
   325  	// NOTE: IDs must be <= MaxSystemConfigDescID.
   326  	SystemDatabaseID = 1
   327  	// DeprecatedNamespaceTableID was the tableID for the system.namespace table
   328  	// for pre-20.1 clusters.
   329  	DeprecatedNamespaceTableID = 2
   330  	DescriptorTableID          = 3
   331  	UsersTableID               = 4
   332  	ZonesTableID               = 5
   333  	SettingsTableID            = 6
   334  	DescIDSequenceID           = 7
   335  	TenantsTableID             = 8
   336  
   337  	// IDs for the important columns and indexes in the zones table live here to
   338  	// avoid introducing a dependency on sql/sqlbase throughout the codebase.
   339  	ZonesTablePrimaryIndexID = 1
   340  	ZonesTableConfigColumnID = 2
   341  	ZonesTableConfigColFamID = 2
   342  
   343  	DescriptorTablePrimaryKeyIndexID  = 1
   344  	DescriptorTableDescriptorColID    = 2
   345  	DescriptorTableDescriptorColFamID = 2
   346  
   347  	// Reserved IDs for other system tables. Note that some of these IDs refer
   348  	// to "Ranges" instead of a Table - these IDs are needed to store custom
   349  	// configuration for non-table ranges (e.g. Zone Configs).
   350  	// NOTE: IDs must be <= MaxReservedDescID.
   351  	LeaseTableID                         = 11
   352  	EventLogTableID                      = 12
   353  	RangeEventTableID                    = 13
   354  	UITableID                            = 14
   355  	JobsTableID                          = 15
   356  	MetaRangesID                         = 16
   357  	SystemRangesID                       = 17
   358  	TimeseriesRangesID                   = 18
   359  	WebSessionsTableID                   = 19
   360  	TableStatisticsTableID               = 20
   361  	LocationsTableID                     = 21
   362  	LivenessRangesID                     = 22
   363  	RoleMembersTableID                   = 23
   364  	CommentsTableID                      = 24
   365  	ReplicationConstraintStatsTableID    = 25
   366  	ReplicationCriticalLocalitiesTableID = 26
   367  	ReplicationStatsTableID              = 27
   368  	ReportsMetaTableID                   = 28
   369  	PublicSchemaID                       = 29
   370  	// New NamespaceTableID for cluster version >= 20.1
   371  	// Ensures that NamespaceTable does not get gossiped again
   372  	NamespaceTableID = 30
   373  
   374  	ProtectedTimestampsMetaTableID    = 31
   375  	ProtectedTimestampsRecordsTableID = 32
   376  
   377  	RoleOptionsTableID = 33
   378  
   379  	StatementBundleChunksTableID        = 34
   380  	StatementDiagnosticsRequestsTableID = 35
   381  	StatementDiagnosticsTableID         = 36
   382  
   383  	// CommentType is type for system.comments
   384  	DatabaseCommentType = 0
   385  	TableCommentType    = 1
   386  	ColumnCommentType   = 2
   387  	IndexCommentType    = 3
   388  )
   389  
   390  const (
   391  	// SequenceIndexID is the ID of the single index on each special single-column,
   392  	// single-row sequence table.
   393  	SequenceIndexID = 1
   394  	// SequenceColumnFamilyID is the ID of the column family on each special single-column,
   395  	// single-row sequence table.
   396  	SequenceColumnFamilyID = 0
   397  )
   398  
   399  // PseudoTableIDs is the list of ids from above that are not real tables (i.e.
   400  // there's no table descriptor). They're grouped here because the cluster
   401  // bootstrap process needs to create splits for them; splits for the tables
   402  // happen separately.
   403  var PseudoTableIDs = []uint32{MetaRangesID, SystemRangesID, TimeseriesRangesID, LivenessRangesID, PublicSchemaID}