// NewStorePool creates a StorePool and registers the store updating callback // with gossip. func NewStorePool( g *gossip.Gossip, clock *hlc.Clock, rpcContext *rpc.Context, reservationsEnabled bool, timeUntilStoreDead time.Duration, stopper *stop.Stopper, ) *StorePool { sp := &StorePool{ clock: clock, timeUntilStoreDead: timeUntilStoreDead, rpcContext: rpcContext, reservationsEnabled: reservationsEnabled, failedReservationsTimeout: envutil.EnvOrDefaultDuration("failed_reservation_timeout", defaultFailedReservationsTimeout), declinedReservationsTimeout: envutil.EnvOrDefaultDuration("declined_reservation_timeout", defaultDeclinedReservationsTimeout), reserveRPCTimeout: envutil.EnvOrDefaultDuration("reserve_rpc_timeout", defaultReserveRPCTimeout), } sp.mu.stores = make(map[roachpb.StoreID]*storeDetail) heap.Init(&sp.mu.queue) storeRegex := gossip.MakePrefixPattern(gossip.KeyStorePrefix) g.RegisterCallback(storeRegex, sp.storeGossipUpdate) sp.start(stopper) return sp }
// NewStorePool creates a StorePool and registers the store updating callback // with gossip. func NewStorePool( g *gossip.Gossip, clock *hlc.Clock, rpcContext *rpc.Context, reservationsEnabled bool, timeUntilStoreDead time.Duration, stopper *stop.Stopper, ) *StorePool { sp := &StorePool{ clock: clock, timeUntilStoreDead: timeUntilStoreDead, rpcContext: rpcContext, reservationsEnabled: reservationsEnabled, failedReservationsTimeout: envutil.EnvOrDefaultDuration("COCKROACH_FAILED_RESERVATION_TIMEOUT", defaultFailedReservationsTimeout), declinedReservationsTimeout: envutil.EnvOrDefaultDuration("COCKROACH_DECLINED_RESERVATION_TIMEOUT", defaultDeclinedReservationsTimeout), reserveRPCTimeout: envutil.EnvOrDefaultDuration("COCKROACH_RESERVE_RPC_TIMEOUT", defaultReserveRPCTimeout), resolver: GossipAddressResolver(g), } sp.mu.stores = make(map[roachpb.StoreID]*storeDetail) heap.Init(&sp.mu.queue) storeRegex := gossip.MakePrefixPattern(gossip.KeyStorePrefix) g.RegisterCallback(storeRegex, sp.storeGossipUpdate) deadReplicasRegex := gossip.MakePrefixPattern(gossip.KeyDeadReplicasPrefix) g.RegisterCallback(deadReplicasRegex, sp.deadReplicasGossipUpdate) sp.start(stopper) return sp }
// startGossip loops on a periodic ticker to gossip node-related // information. Starts a goroutine to loop until the node is closed. func (n *Node) startGossip(ctx context.Context, stopper *stop.Stopper) { stopper.RunWorker(func() { gossipStoresInterval := envutil.EnvOrDefaultDuration("gossip_stores_interval", gossip.DefaultGossipStoresInterval) statusTicker := time.NewTicker(gossipStatusInterval) storesTicker := time.NewTicker(gossipStoresInterval) nodeTicker := time.NewTicker(gossipNodeDescriptorInterval) defer storesTicker.Stop() defer nodeTicker.Stop() n.gossipStores(ctx) // one-off run before going to sleep for { select { case <-statusTicker.C: n.ctx.Gossip.LogStatus() case <-storesTicker.C: n.gossipStores(ctx) case <-nodeTicker.C: if err := n.ctx.Gossip.SetNodeDescriptor(&n.Descriptor); err != nil { log.Warningf(ctx, "couldn't gossip descriptor for node %d: %s", n.Descriptor.NodeID, err) } case <-stopper.ShouldStop(): return } } }) }
// Open creates options and opens the database. If the database // doesn't yet exist at the specified directory, one is initialized // from scratch. The RocksDB Open and Close methods are reference // counted such that subsequent Open calls to an already opened // RocksDB instance only bump the reference count. The RocksDB is only // closed when a sufficient number of Close calls are performed to // bring the reference count down to 0. func (r *RocksDB) Open() error { if r.rdb != nil { return nil } if r.memtableBudget < minMemtableBudget { return util.Errorf("memtable budget must be at least %s: %s", humanize.IBytes(minMemtableBudget), humanizeutil.IBytes(r.memtableBudget)) } var ver storageVersion if len(r.dir) != 0 { log.Infof("opening rocksdb instance at %q", r.dir) // Check the version number. var err error if ver, err = getVersion(r.dir); err != nil { return err } if ver < versionMinimum || ver > versionCurrent { // Instead of an error, we should call a migration if possible when // one is needed immediately following the DBOpen call. return fmt.Errorf("incompatible rocksdb data version, current:%d, on disk:%d, minimum:%d", versionCurrent, ver, versionMinimum) } } else { log.Infof("opening in memory rocksdb instance") // In memory dbs are always current. ver = versionCurrent } status := C.DBOpen(&r.rdb, goToCSlice([]byte(r.dir)), C.DBOptions{ cache_size: C.uint64_t(r.cacheSize), memtable_budget: C.uint64_t(r.memtableBudget), block_size: C.uint64_t(envutil.EnvOrDefaultBytes("rocksdb_block_size", defaultBlockSize)), wal_ttl_seconds: C.uint64_t(envutil.EnvOrDefaultDuration("rocksdb_wal_ttl", 0).Seconds()), allow_os_buffer: C.bool(true), logging_enabled: C.bool(log.V(3)), }) if err := statusToError(status); err != nil { return util.Errorf("could not open rocksdb instance: %s", err) } // Update or add the version file if needed. if ver < versionCurrent { if err := writeVersionFile(r.dir); err != nil { return err } } // Start a goroutine that will finish when the underlying handle // is deallocated. This is used to check a leak in tests. go func() { <-r.deallocated }() r.stopper.AddCloser(r) return nil }
// MakeAllocator creates a new allocator using the specified StorePool. func MakeAllocator(storePool *StorePool, options AllocatorOptions) Allocator { var randSource rand.Source if options.Deterministic { randSource = rand.NewSource(777) } else { randSource = rand.NewSource(rand.Int63()) } return Allocator{ storePool: storePool, options: options, randGen: makeAllocatorRand(randSource), minRebalanceInterval: envutil.EnvOrDefaultDuration("min_rebalance_interval", defaultMinRebalanceInterval), maxRebalanceInterval: envutil.EnvOrDefaultDuration("max_rebalance_interval", defaultMaxRebalanceInterval), } }
func initFakeTime() { if offset := envutil.EnvOrDefaultDuration("COCKROACH_SIMULATED_OFFSET", 0); offset == 0 { nowFunc = time.Now } else { nowFunc = func() time.Time { return time.Now().Add(offset) } } }
func initFakeTime() { if offset := envutil.EnvOrDefaultDuration("simulated_offset", 0); offset == 0 { nowFunc = time.Now } else { nowFunc = func() time.Time { return time.Now().Add(offset) } } }
// readEnvironmentVariables populates all context values that are environment // variable based. Note that this only happens when initializing a node and not // when NewContext is called. func (ctx *Context) readEnvironmentVariables() { // cockroach-linearizable ctx.Linearizable = envutil.EnvOrDefaultBool("linearizable", ctx.Linearizable) ctx.ConsistencyCheckPanicOnFailure = envutil.EnvOrDefaultBool("consistency_check_panic_on_failure", ctx.ConsistencyCheckPanicOnFailure) ctx.MaxOffset = envutil.EnvOrDefaultDuration("max_offset", ctx.MaxOffset) ctx.MetricsSampleInterval = envutil.EnvOrDefaultDuration("metrics_sample_interval", ctx.MetricsSampleInterval) ctx.ScanInterval = envutil.EnvOrDefaultDuration("scan_interval", ctx.ScanInterval) ctx.ScanMaxIdleTime = envutil.EnvOrDefaultDuration("scan_max_idle_time", ctx.ScanMaxIdleTime) ctx.TimeUntilStoreDead = envutil.EnvOrDefaultDuration("time_until_store_dead", ctx.TimeUntilStoreDead) ctx.ConsistencyCheckInterval = envutil.EnvOrDefaultDuration("consistency_check_interval", ctx.ConsistencyCheckInterval) }
// readEnvironmentVariables populates all context values that are environment // variable based. Note that this only happens when initializing a node and not // when NewContext is called. func (ctx *Context) readEnvironmentVariables() { // cockroach-linearizable ctx.Linearizable = envutil.EnvOrDefaultBool("COCKROACH_LINEARIZABLE", ctx.Linearizable) ctx.ConsistencyCheckPanicOnFailure = envutil.EnvOrDefaultBool("COCKROACH_CONSISTENCY_CHECK_PANIC_ON_FAILURE", ctx.ConsistencyCheckPanicOnFailure) ctx.MaxOffset = envutil.EnvOrDefaultDuration("COCKROACH_MAX_OFFSET", ctx.MaxOffset) ctx.MetricsSampleInterval = envutil.EnvOrDefaultDuration("COCKROACH_METRICS_SAMPLE_INTERVAL", ctx.MetricsSampleInterval) ctx.ScanInterval = envutil.EnvOrDefaultDuration("COCKROACH_SCAN_INTERVAL", ctx.ScanInterval) ctx.ScanMaxIdleTime = envutil.EnvOrDefaultDuration("COCKROACH_SCAN_MAX_IDLE_TIME", ctx.ScanMaxIdleTime) ctx.TimeUntilStoreDead = envutil.EnvOrDefaultDuration("COCKROACH_TIME_UNTIL_STORE_DEAD", ctx.TimeUntilStoreDead) ctx.ConsistencyCheckInterval = envutil.EnvOrDefaultDuration("COCKROACH_CONSISTENCY_CHECK_INTERVAL", ctx.ConsistencyCheckInterval) // TODO(bram): remove ReservationsEnabled once we've completed testing the // feature. ctx.ReservationsEnabled = envutil.EnvOrDefaultBool("COCKROACH_RESERVATIONS_ENABLED", ctx.ReservationsEnabled) }
// readEnvironmentVariables populates all context values that are environment // variable based. Note that this only happens when initializing a node and not // when NewContext is called. func (ctx *Context) readEnvironmentVariables() { // cockroach-linearizable ctx.Linearizable = envutil.EnvOrDefaultBool("linearizable", ctx.Linearizable) ctx.ConsistencyCheckPanicOnFailure = envutil.EnvOrDefaultBool("consistency_check_panic_on_failure", ctx.ConsistencyCheckPanicOnFailure) ctx.MaxOffset = envutil.EnvOrDefaultDuration("max_offset", ctx.MaxOffset) ctx.MetricsSampleInterval = envutil.EnvOrDefaultDuration("metrics_sample_interval", ctx.MetricsSampleInterval) ctx.ScanInterval = envutil.EnvOrDefaultDuration("scan_interval", ctx.ScanInterval) ctx.ScanMaxIdleTime = envutil.EnvOrDefaultDuration("scan_max_idle_time", ctx.ScanMaxIdleTime) ctx.TimeUntilStoreDead = envutil.EnvOrDefaultDuration("time_until_store_dead", ctx.TimeUntilStoreDead) ctx.ConsistencyCheckInterval = envutil.EnvOrDefaultDuration("consistency_check_interval", ctx.ConsistencyCheckInterval) // TODO(bram): remove ReservationsEnabled once we've completed testing the // feature. ctx.ReservationsEnabled = envutil.EnvOrDefaultBool("reservations_enabled", ctx.ReservationsEnabled) }
func initFakeTime() { offset := envutil.EnvOrDefaultDuration("simulated_offset", 0) SetTimeOffset(offset) }
"github.com/cockroachdb/cockroach/util/envutil" "github.com/cockroachdb/cockroach/util/log" "github.com/cockroachdb/cockroach/util/retry" "github.com/cockroachdb/cockroach/util/timeutil" "github.com/cockroachdb/cockroach/util/tracing" basictracer "github.com/opentracing/basictracer-go" opentracing "github.com/opentracing/opentracing-go" ) // COCKROACH_TRACE_SQL=duration can be used to log SQL transactions that take // longer than duration to complete. For example, COCKROACH_TRACE_SQL=1s will // log the trace for any transaction that takes 1s or longer. To log traces for // all transactions use COCKROACH_TRACE_SQL=1ns. Note that any positive // duration will enable tracing and will slow down all execution because traces // are gathered for all transactions even if they are not output. var traceSQLDuration = envutil.EnvOrDefaultDuration("COCKROACH_TRACE_SQL", 0) var traceSQL = traceSQLDuration > 0 // Session contains the state of a SQL client connection. // Create instances using NewSession(). type Session struct { Database string User string Syntax int32 // Info about the open transaction (if any). TxnState txnState planner planner PreparedStatements PreparedStatements PreparedPortals PreparedPortals