github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sessiondatapb/local_only_session_data.go (about)

     1  // Copyright 2021 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 sessiondatapb
    12  
    13  import (
    14  	"fmt"
    15  	"strings"
    16  
    17  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/admission/admissionpb"
    18  	"github.com/cockroachdb/errors"
    19  )
    20  
    21  // ExperimentalDistSQLPlanningMode controls if and when the opt-driven DistSQL
    22  // planning is used to create physical plans.
    23  type ExperimentalDistSQLPlanningMode int64
    24  
    25  const (
    26  	// ExperimentalDistSQLPlanningOff means that we always use the old path of
    27  	// going from opt.Expr to planNodes and then to processor specs.
    28  	ExperimentalDistSQLPlanningOff ExperimentalDistSQLPlanningMode = iota
    29  	// ExperimentalDistSQLPlanningOn means that we will attempt to use the new
    30  	// path for performing DistSQL planning in the optimizer, and if that
    31  	// doesn't succeed for some reason, we will fallback to the old path.
    32  	ExperimentalDistSQLPlanningOn
    33  	// ExperimentalDistSQLPlanningAlways means that we will only use the new path,
    34  	// and if it fails for any reason, the query will fail as well.
    35  	ExperimentalDistSQLPlanningAlways
    36  )
    37  
    38  func (m ExperimentalDistSQLPlanningMode) String() string {
    39  	switch m {
    40  	case ExperimentalDistSQLPlanningOff:
    41  		return "off"
    42  	case ExperimentalDistSQLPlanningOn:
    43  		return "on"
    44  	case ExperimentalDistSQLPlanningAlways:
    45  		return "always"
    46  	default:
    47  		return fmt.Sprintf("invalid (%d)", m)
    48  	}
    49  }
    50  
    51  // ExperimentalDistSQLPlanningModeFromString converts a string into a
    52  // ExperimentalDistSQLPlanningMode. False is returned if the conversion was
    53  // unsuccessful.
    54  func ExperimentalDistSQLPlanningModeFromString(val string) (ExperimentalDistSQLPlanningMode, bool) {
    55  	var m ExperimentalDistSQLPlanningMode
    56  	switch strings.ToUpper(val) {
    57  	case "OFF":
    58  		m = ExperimentalDistSQLPlanningOff
    59  	case "ON":
    60  		m = ExperimentalDistSQLPlanningOn
    61  	case "ALWAYS":
    62  		m = ExperimentalDistSQLPlanningAlways
    63  	default:
    64  		return 0, false
    65  	}
    66  	return m, true
    67  }
    68  
    69  // DistSQLExecMode controls if and when the Executor distributes queries.
    70  // Since 2.1, we run everything through the DistSQL infrastructure,
    71  // and these settings control whether to use a distributed plan, or use a plan
    72  // that only involves local DistSQL processors.
    73  type DistSQLExecMode int64
    74  
    75  const (
    76  	// DistSQLOff means that we never distribute queries.
    77  	DistSQLOff DistSQLExecMode = iota
    78  	// DistSQLAuto means that we automatically decide on a case-by-case basis if
    79  	// we distribute queries.
    80  	DistSQLAuto
    81  	// DistSQLOn means that we distribute queries that are supported.
    82  	DistSQLOn
    83  	// DistSQLAlways means that we only distribute; unsupported queries fail.
    84  	DistSQLAlways
    85  )
    86  
    87  func (m DistSQLExecMode) String() string {
    88  	switch m {
    89  	case DistSQLOff:
    90  		return "off"
    91  	case DistSQLAuto:
    92  		return "auto"
    93  	case DistSQLOn:
    94  		return "on"
    95  	case DistSQLAlways:
    96  		return "always"
    97  	default:
    98  		return fmt.Sprintf("invalid (%d)", m)
    99  	}
   100  }
   101  
   102  // DistSQLExecModeFromString converts a string into a DistSQLExecMode
   103  func DistSQLExecModeFromString(val string) (_ DistSQLExecMode, ok bool) {
   104  	switch strings.ToUpper(val) {
   105  	case "OFF":
   106  		return DistSQLOff, true
   107  	case "AUTO":
   108  		return DistSQLAuto, true
   109  	case "ON":
   110  		return DistSQLOn, true
   111  	case "ALWAYS":
   112  		return DistSQLAlways, true
   113  	default:
   114  		return 0, false
   115  	}
   116  }
   117  
   118  // SerialNormalizationMode controls if and when the Executor uses DistSQL.
   119  // NB: The values of the enums must be stable across releases.
   120  type SerialNormalizationMode int64
   121  
   122  const (
   123  	// SerialUsesRowID means use INT NOT NULL DEFAULT unique_rowid().
   124  	SerialUsesRowID SerialNormalizationMode = 0
   125  	// SerialUsesVirtualSequences means create a virtual sequence and
   126  	// use INT NOT NULL DEFAULT nextval(...).
   127  	SerialUsesVirtualSequences SerialNormalizationMode = 1
   128  	// SerialUsesSQLSequences means create a regular SQL sequence and
   129  	// use INT NOT NULL DEFAULT nextval(...). Each call to nextval()
   130  	// is a distributed call to kv. This minimizes the size of gaps
   131  	// between successive sequence numbers (which occur due to
   132  	// node failures or errors), but the multiple kv calls
   133  	// can impact performance negatively.
   134  	SerialUsesSQLSequences SerialNormalizationMode = 2
   135  	// SerialUsesCachedSQLSequences is identical to SerialUsesSQLSequences with
   136  	// the exception that nodes can cache sequence values. This significantly
   137  	// reduces contention and distributed calls to kv, which results in better
   138  	// performance. Gaps between sequences may be larger as a result of cached
   139  	// values being lost to errors and/or node failures.
   140  	SerialUsesCachedSQLSequences SerialNormalizationMode = 3
   141  	// SerialUsesUnorderedRowID means use INT NOT NULL DEFAULT unordered_unique_rowid().
   142  	SerialUsesUnorderedRowID SerialNormalizationMode = 4
   143  )
   144  
   145  func (m SerialNormalizationMode) String() string {
   146  	switch m {
   147  	case SerialUsesRowID:
   148  		return "rowid"
   149  	case SerialUsesUnorderedRowID:
   150  		return "unordered_rowid"
   151  	case SerialUsesVirtualSequences:
   152  		return "virtual_sequence"
   153  	case SerialUsesSQLSequences:
   154  		return "sql_sequence"
   155  	case SerialUsesCachedSQLSequences:
   156  		return "sql_sequence_cached"
   157  	default:
   158  		return fmt.Sprintf("invalid (%d)", m)
   159  	}
   160  }
   161  
   162  // SerialNormalizationModeFromString converts a string into a SerialNormalizationMode
   163  func SerialNormalizationModeFromString(val string) (_ SerialNormalizationMode, ok bool) {
   164  	switch strings.ToUpper(val) {
   165  	case "ROWID":
   166  		return SerialUsesRowID, true
   167  	case "UNORDERED_ROWID":
   168  		return SerialUsesUnorderedRowID, true
   169  	case "VIRTUAL_SEQUENCE":
   170  		return SerialUsesVirtualSequences, true
   171  	case "SQL_SEQUENCE":
   172  		return SerialUsesSQLSequences, true
   173  	case "SQL_SEQUENCE_CACHED":
   174  		return SerialUsesCachedSQLSequences, true
   175  	default:
   176  		return 0, false
   177  	}
   178  }
   179  
   180  // NewSchemaChangerMode controls if and when the new schema changer (in
   181  // sql/schemachanger) is in use.
   182  type NewSchemaChangerMode int64
   183  
   184  const (
   185  	// UseNewSchemaChangerOff means that we never use the new schema changer.
   186  	UseNewSchemaChangerOff NewSchemaChangerMode = iota
   187  	// UseNewSchemaChangerOn means that we use the new schema changer for
   188  	// supported statements in implicit transactions, but fall back to the old
   189  	// schema changer otherwise.
   190  	UseNewSchemaChangerOn
   191  	// UseNewSchemaChangerUnsafe means that we attempt to use the new schema
   192  	// changer for implemented statements including ones which aren't production
   193  	// ready. Used for testing/development.
   194  	UseNewSchemaChangerUnsafe
   195  	// UseNewSchemaChangerUnsafeAlways means that we attempt to use the new schema
   196  	// changer for all statements and return errors for unsupported statements.
   197  	// Used for testing/development.
   198  	UseNewSchemaChangerUnsafeAlways
   199  )
   200  
   201  func (m NewSchemaChangerMode) String() string {
   202  	switch m {
   203  	case UseNewSchemaChangerOff:
   204  		return "off"
   205  	case UseNewSchemaChangerOn:
   206  		return "on"
   207  	case UseNewSchemaChangerUnsafe:
   208  		return "unsafe"
   209  	case UseNewSchemaChangerUnsafeAlways:
   210  		return "unsafe_always"
   211  	default:
   212  		return fmt.Sprintf("invalid (%d)", m)
   213  	}
   214  }
   215  
   216  // NewSchemaChangerModeFromString converts a string into a NewSchemaChangerMode
   217  func NewSchemaChangerModeFromString(val string) (_ NewSchemaChangerMode, ok bool) {
   218  	switch strings.ToUpper(val) {
   219  	case "OFF":
   220  		return UseNewSchemaChangerOff, true
   221  	case "ON":
   222  		return UseNewSchemaChangerOn, true
   223  	case "UNSAFE":
   224  		return UseNewSchemaChangerUnsafe, true
   225  	case "UNSAFE_ALWAYS":
   226  		return UseNewSchemaChangerUnsafeAlways, true
   227  	default:
   228  		return 0, false
   229  	}
   230  }
   231  
   232  // DescriptorValidationMode controls if and when descriptors are validated.
   233  type DescriptorValidationMode int64
   234  
   235  const (
   236  	// DescriptorValidationOn means that we always validate descriptors,
   237  	// both when reading from storage and when writing to storage.
   238  	DescriptorValidationOn DescriptorValidationMode = iota
   239  	// DescriptorValidationOff means that we never validate descriptors.
   240  	DescriptorValidationOff
   241  	// DescriptorValidationReadOnly means that we validate descriptors when
   242  	// reading from storage, but not when writing to storage.
   243  	DescriptorValidationReadOnly
   244  )
   245  
   246  func (m DescriptorValidationMode) String() string {
   247  	switch m {
   248  	case DescriptorValidationOn:
   249  		return "on"
   250  	case DescriptorValidationOff:
   251  		return "off"
   252  	case DescriptorValidationReadOnly:
   253  		return "read_only"
   254  	default:
   255  		return fmt.Sprintf("invalid (%d)", m)
   256  	}
   257  }
   258  
   259  // DescriptorValidationModeFromString converts a string into a
   260  // DescriptorValidationMode.
   261  func DescriptorValidationModeFromString(val string) (_ DescriptorValidationMode, ok bool) {
   262  	switch strings.ToUpper(val) {
   263  	case "ON":
   264  		return DescriptorValidationOn, true
   265  	case "OFF":
   266  		return DescriptorValidationOff, true
   267  	case "READ_ONLY":
   268  		return DescriptorValidationReadOnly, true
   269  	default:
   270  		return 0, false
   271  	}
   272  }
   273  
   274  // QoSLevel controls the level of admission control to use for new SQL requests.
   275  type QoSLevel admissionpb.WorkPriority
   276  
   277  const (
   278  	// SystemLow denotes the minimum system QoS level, which is not settable as a
   279  	// session default_transaction_quality_of_service value.
   280  	SystemLow = QoSLevel(admissionpb.LowPri)
   281  
   282  	// TTLStatsLow denotes a QoS level used internally by the TTL feature, which
   283  	// is not settable as a session default_transaction_quality_of_service value.
   284  	TTLStatsLow = QoSLevel(admissionpb.TTLLowPri)
   285  
   286  	// TTLLow denotes a QoS level used internally by the TTL feature, which is not
   287  	// settable as a session default_transaction_quality_of_service value.
   288  	TTLLow = QoSLevel(admissionpb.TTLLowPri)
   289  
   290  	// UserLow denotes an end user QoS level lower than the default.
   291  	UserLow = QoSLevel(admissionpb.UserLowPri)
   292  
   293  	// Normal denotes an end user QoS level unchanged from the default.
   294  	Normal = QoSLevel(admissionpb.NormalPri)
   295  
   296  	// LockingNormal denotes an internal increased priority for normal
   297  	// transactions that are acquiring locks.
   298  	LockingNormal = QoSLevel(admissionpb.LockingNormalPri)
   299  
   300  	// UserHigh denotes an end user QoS level higher than the default.
   301  	UserHigh = QoSLevel(admissionpb.UserHighPri)
   302  
   303  	// LockingHigh denotes an internal increased priority for UserHigh
   304  	// transactions that are acquiring locks.
   305  	LockingHigh = QoSLevel(admissionpb.LockingUserHighPri)
   306  
   307  	// SystemHigh denotes the maximum system QoS level, which is not settable as a
   308  	// session default_transaction_quality_of_service value.
   309  	SystemHigh = QoSLevel(admissionpb.HighPri)
   310  )
   311  
   312  const (
   313  	// NormalName is the external session setting string value to use to mean
   314  	// Normal QoS level.
   315  	NormalName = "regular"
   316  
   317  	// UserHighName is the external session setting string value to use to mean
   318  	// UserHigh QoS level.
   319  	UserHighName = "critical"
   320  
   321  	// UserLowName is the external session setting string value to use to mean
   322  	// UserLow QoS level.
   323  	UserLowName = "background"
   324  
   325  	// SystemHighName is the string value to display indicating a SystemHigh
   326  	// QoS level.
   327  	SystemHighName = "maximum"
   328  
   329  	// SystemLowName is the string value to display indicating a SystemLow
   330  	// QoS level.
   331  	SystemLowName = "minimum"
   332  
   333  	// TTLLowName is the string value to display indicating a TTLLow QoS level.
   334  	TTLLowName = "ttl_low"
   335  
   336  	// LockingNormalName is the string value to display indicating a
   337  	// LockingNormal QoS level.
   338  	LockingNormalName = "locking-normal"
   339  
   340  	// LockingHighName is the string value to display indicating a LockingHigh
   341  	// QoS level.
   342  	LockingHighName = "locking-high"
   343  )
   344  
   345  var qosLevelsDict = map[QoSLevel]string{
   346  	SystemLow:     SystemLowName,
   347  	TTLLow:        TTLLowName,
   348  	UserLow:       UserLowName,
   349  	Normal:        NormalName,
   350  	LockingNormal: LockingNormalName,
   351  	UserHigh:      UserHighName,
   352  	LockingHigh:   LockingHighName,
   353  	SystemHigh:    SystemHighName,
   354  }
   355  
   356  // ParseQoSLevelFromString converts a string into a QoSLevel
   357  func ParseQoSLevelFromString(val string) (_ QoSLevel, ok bool) {
   358  	switch strings.ToUpper(val) {
   359  	case strings.ToUpper(UserHighName):
   360  		return UserHigh, true
   361  	case strings.ToUpper(UserLowName):
   362  		return UserLow, true
   363  	case strings.ToUpper(NormalName):
   364  		return Normal, true
   365  	default:
   366  		return 0, false
   367  	}
   368  }
   369  
   370  // String prints the string representation of the
   371  // default_transaction_quality_of_service session setting.
   372  func (e QoSLevel) String() string {
   373  	if name, ok := qosLevelsDict[e]; ok {
   374  		return name
   375  	}
   376  	return fmt.Sprintf("%d", int(e))
   377  }
   378  
   379  // ToQoSLevelString interprets an int32 value as a QoSLevel and returns its
   380  // String representation.
   381  func ToQoSLevelString(value int32) string {
   382  	if value > int32(SystemHigh) || value < int32(SystemLow) {
   383  		return fmt.Sprintf("%d", value)
   384  	}
   385  	qosLevel := QoSLevel(value)
   386  	return qosLevel.String()
   387  }
   388  
   389  // Validate checks for a valid user QoSLevel setting before returning it.
   390  func (e QoSLevel) Validate() QoSLevel {
   391  	switch e {
   392  	case Normal, UserHigh, UserLow:
   393  		return e
   394  	default:
   395  		panic(errors.AssertionFailedf("use of illegal user QoSLevel: %s", e.String()))
   396  	}
   397  }
   398  
   399  // ValidateInternal checks for a valid internal QoSLevel setting before
   400  // returning it.
   401  func (e QoSLevel) ValidateInternal() QoSLevel {
   402  	if _, ok := qosLevelsDict[e]; ok {
   403  		return e
   404  	}
   405  	panic(errors.AssertionFailedf("use of illegal internal QoSLevel: %s", e.String()))
   406  }