github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/execinfra/server_config.go (about)

     1  // Copyright 2019 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  // This file lives here instead of sql/distsql to avoid an import cycle.
    12  
    13  package execinfra
    14  
    15  import (
    16  	"github.com/cockroachdb/cockroach/pkg/base"
    17  	"github.com/cockroachdb/cockroach/pkg/gossip"
    18  	"github.com/cockroachdb/cockroach/pkg/jobs"
    19  	"github.com/cockroachdb/cockroach/pkg/keys"
    20  	"github.com/cockroachdb/cockroach/pkg/kv"
    21  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/diskmap"
    22  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
    23  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
    24  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    25  	"github.com/cockroachdb/cockroach/pkg/rpc"
    26  	"github.com/cockroachdb/cockroach/pkg/rpc/nodedialer"
    27  	"github.com/cockroachdb/cockroach/pkg/settings"
    28  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    29  	"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
    30  	"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
    31  	"github.com/cockroachdb/cockroach/pkg/storage/cloud"
    32  	"github.com/cockroachdb/cockroach/pkg/storage/fs"
    33  	"github.com/cockroachdb/cockroach/pkg/util/log"
    34  	"github.com/cockroachdb/cockroach/pkg/util/mon"
    35  	"github.com/cockroachdb/cockroach/pkg/util/stop"
    36  	"github.com/marusama/semaphore"
    37  )
    38  
    39  // Version identifies the distsql protocol version.
    40  //
    41  // This version is separate from the main CockroachDB version numbering; it is
    42  // only changed when the distsql API changes.
    43  //
    44  // The planner populates the version in SetupFlowRequest.
    45  // A server only accepts requests with versions in the range MinAcceptedVersion
    46  // to Version.
    47  //
    48  // Is is possible used to provide a "window" of compatibility when new features are
    49  // added. Example:
    50  //  - we start with Version=1; distsql servers with version 1 only accept
    51  //    requests with version 1.
    52  //  - a new distsql feature is added; Version is bumped to 2. The
    53  //    planner does not yet use this feature by default; it still issues
    54  //    requests with version 1.
    55  //  - MinAcceptedVersion is still 1, i.e. servers with version 2
    56  //    accept both versions 1 and 2.
    57  //  - after an upgrade cycle, we can enable the feature in the planner,
    58  //    requiring version 2.
    59  //  - at some later point, we can choose to deprecate version 1 and have
    60  //    servers only accept versions >= 2 (by setting
    61  //    MinAcceptedVersion to 2).
    62  //
    63  // ATTENTION: When updating these fields, add to version_history.txt explaining
    64  // what changed.
    65  const Version execinfrapb.DistSQLVersion = 29
    66  
    67  // MinAcceptedVersion is the oldest version that the server is
    68  // compatible with; see above.
    69  const MinAcceptedVersion execinfrapb.DistSQLVersion = 29
    70  
    71  // SettingWorkMemBytes is a cluster setting that determines the maximum amount
    72  // of RAM that a processor can use.
    73  var SettingWorkMemBytes = settings.RegisterByteSizeSetting(
    74  	"sql.distsql.temp_storage.workmem",
    75  	"maximum amount of memory in bytes a processor can use before falling back to temp storage",
    76  	64*1024*1024, /* 64MB */
    77  )
    78  
    79  // ServerConfig encompasses the configuration required to create a
    80  // DistSQLServer.
    81  type ServerConfig struct {
    82  	log.AmbientContext
    83  
    84  	Settings     *cluster.Settings
    85  	RuntimeStats RuntimeStats
    86  
    87  	ClusterID   *base.ClusterIDContainer
    88  	ClusterName string
    89  
    90  	// NodeID is the id of the node on which this Server is running.
    91  	NodeID *base.SQLIDContainer
    92  
    93  	// Codec is capable of encoding and decoding sql table keys.
    94  	Codec keys.SQLCodec
    95  
    96  	// DB is a handle to the cluster.
    97  	DB *kv.DB
    98  	// Executor can be used to run "internal queries". Note that Flows also have
    99  	// access to an executor in the EvalContext. That one is "session bound"
   100  	// whereas this one isn't.
   101  	Executor sqlutil.InternalExecutor
   102  
   103  	RPCContext   *rpc.Context
   104  	Stopper      *stop.Stopper
   105  	TestingKnobs TestingKnobs
   106  
   107  	// ParentMemoryMonitor is normally the root SQL monitor. It should only be
   108  	// used when setting up a server, or in tests.
   109  	ParentMemoryMonitor *mon.BytesMonitor
   110  
   111  	// TempStorage is used by some DistSQL processors to store rows when the
   112  	// working set is larger than can be stored in memory.
   113  	TempStorage diskmap.Factory
   114  
   115  	// TempStoragePath is the path where the vectorized execution engine should
   116  	// create files using TempFS.
   117  	TempStoragePath string
   118  
   119  	// TempFS is used by the vectorized execution engine to store columns when the
   120  	// working set is larger than can be stored in memory.
   121  	TempFS fs.FS
   122  
   123  	// VecFDSemaphore is a weighted semaphore that restricts the number of open
   124  	// file descriptors in the vectorized engine.
   125  	VecFDSemaphore semaphore.Semaphore
   126  
   127  	// BulkAdder is used by some processors to bulk-ingest data as SSTs.
   128  	BulkAdder kvserverbase.BulkAdderFactory
   129  
   130  	// DiskMonitor is used to monitor temporary storage disk usage. Actual disk
   131  	// space used will be a small multiple (~1.1) of this because of RocksDB
   132  	// space amplification.
   133  	DiskMonitor *mon.BytesMonitor
   134  
   135  	Metrics *DistSQLMetrics
   136  
   137  	// JobRegistry manages jobs being used by this Server.
   138  	JobRegistry *jobs.Registry
   139  
   140  	// LeaseManager is a *sql.LeaseManager. It's stored as an `interface{}` due
   141  	// to package dependency cycles
   142  	LeaseManager interface{}
   143  
   144  	// A handle to gossip used to broadcast the node's DistSQL version and
   145  	// draining state.
   146  	Gossip gossip.DeprecatedGossip
   147  
   148  	NodeDialer *nodedialer.Dialer
   149  
   150  	// SessionBoundInternalExecutorFactory is used to construct session-bound
   151  	// executors. The idea is that a higher-layer binds some of the arguments
   152  	// required, so that users of ServerConfig don't have to care about them.
   153  	SessionBoundInternalExecutorFactory sqlutil.SessionBoundInternalExecutorFactory
   154  
   155  	ExternalStorage        cloud.ExternalStorageFactory
   156  	ExternalStorageFromURI cloud.ExternalStorageFromURIFactory
   157  
   158  	// ProtectedTimestampProvider maintains the state of the protected timestamp
   159  	// subsystem. It is queried during the GC process and in the handling of
   160  	// AdminVerifyProtectedTimestampRequest.
   161  	ProtectedTimestampProvider protectedts.Provider
   162  }
   163  
   164  // RuntimeStats is an interface through which the rowexec layer can get
   165  // information about runtime statistics.
   166  type RuntimeStats interface {
   167  	// GetCPUCombinedPercentNorm returns the recent user+system cpu usage,
   168  	// normalized to 0-1 by number of cores.
   169  	GetCPUCombinedPercentNorm() float64
   170  }
   171  
   172  // TestingKnobs are the testing knobs.
   173  type TestingKnobs struct {
   174  	// RunBeforeBackfillChunk is called before executing each chunk of a
   175  	// backfill during a schema change operation. It is called with the
   176  	// current span and returns an error which eventually is returned to the
   177  	// caller of SchemaChanger.exec(). It is called at the start of the
   178  	// backfill function passed into the transaction executing the chunk.
   179  	RunBeforeBackfillChunk func(sp roachpb.Span) error
   180  
   181  	// RunAfterBackfillChunk is called after executing each chunk of a
   182  	// backfill during a schema change operation. It is called just before
   183  	// returning from the backfill function passed into the transaction
   184  	// executing the chunk. It is always called even when the backfill
   185  	// function returns an error, or if the table has already been dropped.
   186  	RunAfterBackfillChunk func()
   187  
   188  	// ForceDiskSpill forces any processors/operators that can fall back to disk
   189  	// to fall back to disk immediately.
   190  	ForceDiskSpill bool
   191  
   192  	// MemoryLimitBytes specifies a maximum amount of working memory that a
   193  	// processor that supports falling back to disk can use. Must be >= 1 to
   194  	// enable. This is a more fine-grained knob than ForceDiskSpill when the
   195  	// available memory needs to be controlled. Once this limit is hit, processors
   196  	// employ their on-disk implementation regardless of applicable cluster
   197  	// settings.
   198  	MemoryLimitBytes int64
   199  
   200  	// DrainFast, if enabled, causes the server to not wait for any currently
   201  	// running flows to complete or give a grace period of minFlowDrainWait
   202  	// to incoming flows to register.
   203  	DrainFast bool
   204  
   205  	// MetadataTestLevel controls whether or not additional metadata test
   206  	// processors are planned, which send additional "RowNum" metadata that is
   207  	// checked by a test receiver on the gateway.
   208  	MetadataTestLevel MetadataTestLevel
   209  
   210  	// DeterministicStats overrides stats which don't have reliable values, like
   211  	// stall time and bytes sent. It replaces them with a zero value.
   212  	DeterministicStats bool
   213  
   214  	// Changefeed contains testing knobs specific to the changefeed system.
   215  	Changefeed base.ModuleTestingKnobs
   216  
   217  	// Flowinfra contains testing knobs specific to the flowinfra system
   218  	Flowinfra base.ModuleTestingKnobs
   219  
   220  	// EnableVectorizedInvariantsChecker, if enabled, will allow for planning
   221  	// the invariant checkers between all columnar operators.
   222  	EnableVectorizedInvariantsChecker bool
   223  
   224  	// Forces bulk adder flush every time a KV batch is processed.
   225  	BulkAdderFlushesEveryBatch bool
   226  }
   227  
   228  // MetadataTestLevel represents the types of queries where metadata test
   229  // processors are planned.
   230  type MetadataTestLevel int
   231  
   232  const (
   233  	// Off represents that no metadata test processors are planned.
   234  	Off MetadataTestLevel = iota
   235  	// NoExplain represents that metadata test processors are planned for all
   236  	// queries except EXPLAIN (DISTSQL) statements.
   237  	NoExplain
   238  	// On represents that metadata test processors are planned for all queries.
   239  	On
   240  )
   241  
   242  // ModuleTestingKnobs is part of the base.ModuleTestingKnobs interface.
   243  func (*TestingKnobs) ModuleTestingKnobs() {}
   244  
   245  // GetWorkMemLimit returns the number of bytes determining the amount of RAM
   246  // available to a single processor or operator.
   247  func GetWorkMemLimit(config *ServerConfig) int64 {
   248  	limit := config.TestingKnobs.MemoryLimitBytes
   249  	if limit <= 0 {
   250  		limit = SettingWorkMemBytes.Get(&config.Settings.SV)
   251  	}
   252  	return limit
   253  }