"github.com/cockroachdb/cockroach/pkg/util/log" ) const ( // RaftLogQueueTimerDuration is the duration between truncations. This needs // to be relatively short so that truncations can keep up with raft log entry // creation. RaftLogQueueTimerDuration = 50 * time.Millisecond // RaftLogQueueStaleThreshold is the minimum threshold for stale raft log // entries. A stale entry is one which all replicas of the range have // progressed past and thus is no longer needed and can be truncated. RaftLogQueueStaleThreshold = 100 ) // raftLogMaxSize limits the maximum size of the Raft log. var raftLogMaxSize = envutil.EnvOrDefaultInt64("COCKROACH_RAFT_LOG_MAX_SIZE", 1<<20 /* 1 MB */) // raftLogQueue manages a queue of replicas slated to have their raft logs // truncated by removing unneeded entries. type raftLogQueue struct { *baseQueue db *client.DB } // newRaftLogQueue returns a new instance of raftLogQueue. func newRaftLogQueue(store *Store, db *client.DB, gossip *gossip.Gossip) *raftLogQueue { rlq := &raftLogQueue{ db: db, } rlq.baseQueue = newBaseQueue( "raftlog", rlq, store, gossip,
// apiServerMessage is the standard body for all HTTP 500 responses. var errAdminAPIError = grpc.Errorf(codes.Internal, "An internal server error "+ "has occurred. Please check your CockroachDB logs for more details.") // A adminServer provides a RESTful HTTP API to administration of // the cockroach cluster. type adminServer struct { server *Server memMonitor mon.MemoryMonitor memMetrics *sql.MemoryMetrics } // noteworthyAdminMemoryUsageBytes is the minimum size tracked by the // admin SQL pool before the pool start explicitly logging overall // usage growth in the log. var noteworthyAdminMemoryUsageBytes = envutil.EnvOrDefaultInt64("COCKROACH_NOTEWORTHY_ADMIN_MEMORY_USAGE", 100*1024) // newAdminServer allocates and returns a new REST server for // administrative APIs. func newAdminServer(s *Server) *adminServer { server := &adminServer{server: s, memMetrics: &s.adminMemMetrics} // TODO(knz): We do not limit memory usage by admin operations // yet. Is this wise? server.memMonitor = mon.MakeUnlimitedMonitor( context.Background(), "admin", nil, nil, noteworthyAdminMemoryUsageBytes, ) return server } // RegisterService registers the GRPC service. func (s *adminServer) RegisterService(g *grpc.Server) {
state := txnState.State if implicitTxn { state = NoTxn } if _, err := result.Rows.AddRow(parser.DTuple{parser.NewDString(state.String())}); err != nil { result.Rows.Close() result.Err = err return result, err } return result, nil } // noteworthyInternalMemoryUsageBytes is the minimum size tracked by // each internal SQL pool before the pool start explicitly logging // overall usage growth in the log. var noteworthyInternalMemoryUsageBytes = envutil.EnvOrDefaultInt64("COCKROACH_NOTEWORTHY_INTERNAL_MEMORY_USAGE", 100*1024) func makeInternalPlanner( opName string, txn *client.Txn, user string, memMetrics *MemoryMetrics, ) *planner { p := makePlanner(opName) p.setTxn(txn) p.resetContexts() p.session.User = user p.session.mon = mon.MakeUnlimitedMonitor(p.session.context, "internal-root", memMetrics.CurBytesCount, memMetrics.MaxBytesHist, noteworthyInternalMemoryUsageBytes) p.session.sessionMon = mon.MakeMonitor("internal-session",
} // Clear interfaces between Session and mon.MemoryMonitor. func (w WrappedMemoryAccount) Clear() { w.mon.ClearAccount(w.ctx, w.acc) } // ResizeItem interfaces between Session and mon.MemoryMonitor. func (w WrappedMemoryAccount) ResizeItem(oldSize, newSize int64) error { return w.mon.ResizeItem(w.ctx, w.acc, oldSize, newSize) } // noteworthyMemoryUsageBytes is the minimum size tracked by a // transaction or session monitor before the monitor starts explicitly // logging overall usage growth in the log. var noteworthyMemoryUsageBytes = envutil.EnvOrDefaultInt64("COCKROACH_NOTEWORTHY_SESSION_MEMORY_USAGE", 10*1024) // StartMonitor interfaces between Session and mon.MemoryMonitor func (s *Session) StartMonitor(pool *mon.MemoryMonitor, reserved mon.BoundAccount) { // Note: we pass `reserved` to s.mon where it causes `s.mon` to act // as a buffer. This is not done for sessionMon nor TxnState.mon: // these monitors don't start with any buffer, so they'll need to // ask their "parent" for memory as soon as the first // allocation. This is acceptable because the session is single // threaded, and the point of buffering is just to avoid contention. s.mon = mon.MakeMonitor("root", s.memMetrics.CurBytesCount, s.memMetrics.MaxBytesHist, -1, math.MaxInt64) s.mon.Start(s.context, pool, reserved) s.deriveAndStartMonitors()
// Fully-qualified names for metrics. var ( MetaConns = metric.Metadata{Name: "sql.conns"} MetaBytesIn = metric.Metadata{Name: "sql.bytesin"} MetaBytesOut = metric.Metadata{Name: "sql.bytesout"} ) const ( version30 = 196608 versionSSL = 80877103 ) const drainMaxWait = 10 * time.Second // baseSQLMemoryBudget is the amount of memory pre-allocated in each connection. var baseSQLMemoryBudget = envutil.EnvOrDefaultInt64("COCKROACH_BASE_SQL_MEMORY_BUDGET", int64(2.1*float64(mon.DefaultPoolAllocationSize))) // connReservationBatchSize determines for how many connections memory // is pre-reserved at once. var connReservationBatchSize = 5 var ( sslSupported = []byte{'S'} sslUnsupported = []byte{'N'} ) // Server implements the server side of the PostgreSQL wire protocol. type Server struct { AmbientCtx log.AmbientContext cfg *base.Config executor *sql.Executor
// SeedForTests seeds the random number generator and prints the seed // value used. This value can be specified via an environment variable // COCKROACH_RANDOM_SEED=x to reuse the same value later. This function should // be called from TestMain; individual tests should not touch the seed // of the global random number generator. func SeedForTests() { seed := envutil.EnvOrDefaultInt64("COCKROACH_RANDOM_SEED", NewPseudoSeed()) rand.Seed(seed) log.Printf("Random seed: %v", seed) }
} // maxAllocatedButUnusedMemoryBlocks determines the maximum difference // between the amount of memory used by a monitor and the amount of // memory reserved at the upstream pool before the monitor // relinquishes the memory back to the pool. This is useful so that a // monitor currently at the boundary of a block does not cause // contention when accounts cause its allocation counter to grow and // shrink slightly beyond and beneath an allocation block // boundary. The difference is expressed as a number of blocks of size // `poolAllocationSize`. var maxAllocatedButUnusedMemoryBlocks = envutil.EnvOrDefaultInt("COCKROACH_MAX_ALLOCATED_UNUSED_BLOCKS", 10) // DefaultPoolAllocationSize specifies the unit of allocation used by // a monitor to reserve and release memory to a pool. var DefaultPoolAllocationSize = envutil.EnvOrDefaultInt64("COCKROACH_MEMORY_ALLOCATION_CHUNK_SIZE", 10*1024) // MakeMonitor creates a new monitor. // Arguments: // - name is used to annotate log messages, can be used to distinguish // monitors. // // - curCount and maxHist are the metric objects to update with usage // statistics. // // - increment is the block size used for upstream allocations from // the pool. Note: if set to 0 or lower, the default pool allocation // size is used. // // - noteworthy determines the minimum total allocated size beyond // which the monitor starts to log increases. Use 0 to always log