github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ts/maintenance.go (about) 1 // Copyright 2016 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 ts 12 13 import ( 14 "context" 15 16 "github.com/cockroachdb/cockroach/pkg/kv" 17 "github.com/cockroachdb/cockroach/pkg/kv/kvserver" 18 "github.com/cockroachdb/cockroach/pkg/roachpb" 19 "github.com/cockroachdb/cockroach/pkg/storage" 20 "github.com/cockroachdb/cockroach/pkg/util/hlc" 21 "github.com/cockroachdb/cockroach/pkg/util/mon" 22 ) 23 24 // ContainsTimeSeries returns true if the given key range overlaps the 25 // range of possible time series keys. 26 func (tsdb *DB) ContainsTimeSeries(start, end roachpb.RKey) bool { 27 return !lastTSRKey.Less(start) && !end.Less(firstTSRKey) 28 } 29 30 // MaintainTimeSeries provides a function that can be called from an external 31 // process periodically in order to perform "maintenance" work on time series 32 // data. Currently, this includes computing rollups and pruning data which has 33 // exceeded its retention threshold, as well as computing low-resolution rollups 34 // of data. This system was designed specifically to be used by scanner queue 35 // from the storage package. 36 // 37 // The snapshot should be supplied by a local store, and is used only to 38 // discover the names of time series which are store in that snapshot. The KV 39 // client is then used to interact with data from the time series that are 40 // discovered; this may result in data being deleted, but may also write new 41 // data in the form of rollups. 42 // 43 // The snapshot is used for key discovery (as opposed to the KV client) because 44 // the task of pruning time series is distributed across the cluster to the 45 // individual ranges which contain that time series data. Because replicas of 46 // those ranges are guaranteed to have time series data locally, we can use the 47 // snapshot to quickly obtain a set of keys to be pruned with no network calls. 48 func (tsdb *DB) MaintainTimeSeries( 49 ctx context.Context, 50 snapshot storage.Reader, 51 start, end roachpb.RKey, 52 db *kv.DB, 53 mem *mon.BytesMonitor, 54 budgetBytes int64, 55 now hlc.Timestamp, 56 ) error { 57 series, err := tsdb.findTimeSeries(snapshot, start, end, now) 58 if err != nil { 59 return err 60 } 61 if tsdb.WriteRollups() { 62 qmc := MakeQueryMemoryContext(mem, mem, QueryMemoryOptions{ 63 BudgetBytes: budgetBytes, 64 }) 65 if err := tsdb.rollupTimeSeries(ctx, series, now, qmc); err != nil { 66 return err 67 } 68 } 69 return tsdb.pruneTimeSeries(ctx, db, series, now) 70 } 71 72 // Assert that DB implements the necessary interface from the storage package. 73 var _ kvserver.TimeSeriesDataStore = (*DB)(nil)