github.com/m3db/m3@v1.5.0/src/dbnode/storage/types.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package storage
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"sync"
    27  	"time"
    28  
    29  	"github.com/m3db/m3/src/dbnode/client"
    30  	"github.com/m3db/m3/src/dbnode/encoding"
    31  	"github.com/m3db/m3/src/dbnode/generated/proto/annotation"
    32  	"github.com/m3db/m3/src/dbnode/namespace"
    33  	"github.com/m3db/m3/src/dbnode/persist"
    34  	"github.com/m3db/m3/src/dbnode/persist/fs"
    35  	"github.com/m3db/m3/src/dbnode/persist/fs/commitlog"
    36  	"github.com/m3db/m3/src/dbnode/runtime"
    37  	"github.com/m3db/m3/src/dbnode/sharding"
    38  	"github.com/m3db/m3/src/dbnode/storage/block"
    39  	"github.com/m3db/m3/src/dbnode/storage/bootstrap"
    40  	"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
    41  	"github.com/m3db/m3/src/dbnode/storage/index"
    42  	"github.com/m3db/m3/src/dbnode/storage/index/convert"
    43  	"github.com/m3db/m3/src/dbnode/storage/limits"
    44  	"github.com/m3db/m3/src/dbnode/storage/limits/permits"
    45  	"github.com/m3db/m3/src/dbnode/storage/repair"
    46  	"github.com/m3db/m3/src/dbnode/storage/series"
    47  	"github.com/m3db/m3/src/dbnode/ts"
    48  	"github.com/m3db/m3/src/dbnode/ts/writes"
    49  	"github.com/m3db/m3/src/dbnode/x/xio"
    50  	"github.com/m3db/m3/src/dbnode/x/xpool"
    51  	"github.com/m3db/m3/src/m3ninx/doc"
    52  	"github.com/m3db/m3/src/x/clock"
    53  	"github.com/m3db/m3/src/x/context"
    54  	"github.com/m3db/m3/src/x/ident"
    55  	"github.com/m3db/m3/src/x/instrument"
    56  	"github.com/m3db/m3/src/x/mmap"
    57  	"github.com/m3db/m3/src/x/pool"
    58  	xsync "github.com/m3db/m3/src/x/sync"
    59  	xtime "github.com/m3db/m3/src/x/time"
    60  )
    61  
    62  // PageToken is an opaque paging token.
    63  type PageToken []byte
    64  
    65  // IndexedErrorHandler can handle individual errors based on their index. It
    66  // is used primarily in cases where we need to handle errors in batches, but
    67  // want to avoid an intermediary allocation of []error.
    68  type IndexedErrorHandler interface {
    69  	HandleError(index int, err error)
    70  }
    71  
    72  // Database is a time series database.
    73  type Database interface {
    74  	// Options returns the database options.
    75  	Options() Options
    76  
    77  	// AssignShardSet sets the shard set assignment and returns immediately.
    78  	AssignShardSet(shardSet sharding.ShardSet)
    79  
    80  	// Namespaces returns the namespaces.
    81  	Namespaces() []Namespace
    82  
    83  	// Namespace returns the specified namespace.
    84  	Namespace(ns ident.ID) (Namespace, bool)
    85  
    86  	// Open will open the database for writing and reading.
    87  	Open() error
    88  
    89  	// Close will close the database for writing and reading. Close releases
    90  	// release resources held by owned namespaces.
    91  	Close() error
    92  
    93  	// ShardSet returns the set of shards currently associated with
    94  	// this namespace.
    95  	ShardSet() sharding.ShardSet
    96  
    97  	// Terminate will close the database for writing and reading. Terminate does
    98  	// NOT release any resources held by owned namespaces, instead relying upon
    99  	// the GC to do so.
   100  	Terminate() error
   101  
   102  	// Write value to the database for an ID.
   103  	Write(
   104  		ctx context.Context,
   105  		namespace ident.ID,
   106  		id ident.ID,
   107  		timestamp xtime.UnixNano,
   108  		value float64,
   109  		unit xtime.Unit,
   110  		annotation []byte,
   111  	) error
   112  
   113  	// WriteTagged values to the database for an ID.
   114  	WriteTagged(
   115  		ctx context.Context,
   116  		namespace ident.ID,
   117  		id ident.ID,
   118  		tagResolver convert.TagMetadataResolver,
   119  		timestamp xtime.UnixNano,
   120  		value float64,
   121  		unit xtime.Unit,
   122  		annotation []byte,
   123  	) error
   124  
   125  	// BatchWriter returns a batch writer for the provided namespace that can
   126  	// be used to issue a batch of writes to either WriteBatch
   127  	// or WriteTaggedBatch.
   128  	//
   129  	// Note that when using the BatchWriter the caller owns the lifecycle of the series
   130  	// IDs if they're being pooled its the callers responsibility to return them to the
   131  	// appropriate pool, but the encoded tags and annotations are owned by the
   132  	// writes.WriteBatch itself and will be finalized when the entire writes.WriteBatch is finalized
   133  	// due to their lifecycle being more complicated.
   134  	// Callers can still control the pooling of the encoded tags and annotations by using
   135  	// the SetFinalizeEncodedTagsFn and SetFinalizeAnnotationFn on the WriteBatch itself.
   136  	BatchWriter(namespace ident.ID, batchSize int) (writes.BatchWriter, error)
   137  
   138  	// WriteBatch is the same as Write, but in batch.
   139  	WriteBatch(
   140  		ctx context.Context,
   141  		namespace ident.ID,
   142  		writes writes.BatchWriter,
   143  		errHandler IndexedErrorHandler,
   144  	) error
   145  
   146  	// WriteTaggedBatch is the same as WriteTagged, but in batch.
   147  	WriteTaggedBatch(
   148  		ctx context.Context,
   149  		namespace ident.ID,
   150  		writes writes.BatchWriter,
   151  		errHandler IndexedErrorHandler,
   152  	) error
   153  
   154  	// QueryIDs resolves the given query into known IDs.
   155  	QueryIDs(
   156  		ctx context.Context,
   157  		namespace ident.ID,
   158  		query index.Query,
   159  		opts index.QueryOptions,
   160  	) (index.QueryResult, error)
   161  
   162  	// AggregateQuery resolves the given query into aggregated tags.
   163  	AggregateQuery(
   164  		ctx context.Context,
   165  		namespace ident.ID,
   166  		query index.Query,
   167  		opts index.AggregationOptions,
   168  	) (index.AggregateQueryResult, error)
   169  
   170  	// ReadEncoded retrieves encoded segments for an ID.
   171  	ReadEncoded(
   172  		ctx context.Context,
   173  		namespace ident.ID,
   174  		id ident.ID,
   175  		start, end xtime.UnixNano,
   176  	) (series.BlockReaderIter, error)
   177  
   178  	// FetchBlocks retrieves data blocks for a given id and a list of block
   179  	// start times.
   180  	FetchBlocks(
   181  		ctx context.Context,
   182  		namespace ident.ID,
   183  		shard uint32,
   184  		id ident.ID,
   185  		starts []xtime.UnixNano,
   186  	) ([]block.FetchBlockResult, error)
   187  
   188  	// FetchBlocksMetadataV2 retrieves blocks metadata for a given shard, returns the
   189  	// fetched block metadata results, the next page token, and any error encountered.
   190  	// If we have fetched all the block metadata, we return nil as the next page token.
   191  	FetchBlocksMetadataV2(
   192  		ctx context.Context,
   193  		namespace ident.ID,
   194  		shard uint32,
   195  		start, end xtime.UnixNano,
   196  		limit int64,
   197  		pageToken PageToken,
   198  		opts block.FetchBlocksMetadataOptions,
   199  	) (block.FetchBlocksMetadataResults, PageToken, error)
   200  
   201  	// Bootstrap bootstraps the database.
   202  	Bootstrap() error
   203  
   204  	// IsBootstrapped determines whether the database is bootstrapped.
   205  	IsBootstrapped() bool
   206  
   207  	// IsBootstrappedAndDurable determines whether the database is bootstrapped
   208  	// and durable, meaning that it could recover all data in memory using only
   209  	// the local disk.
   210  	IsBootstrappedAndDurable() bool
   211  
   212  	// IsOverloaded determines whether the database is overloaded.
   213  	IsOverloaded() bool
   214  
   215  	// Repair will issue a repair and return nil on success or error on error.
   216  	Repair() error
   217  
   218  	// Truncate truncates data for the given namespace.
   219  	Truncate(namespace ident.ID) (int64, error)
   220  
   221  	// BootstrapState captures and returns a snapshot of the databases'
   222  	// bootstrap state.
   223  	BootstrapState() DatabaseBootstrapState
   224  
   225  	// FlushState returns the flush state for the specified shard and block start.
   226  	FlushState(namespace ident.ID, shardID uint32, blockStart xtime.UnixNano) (fileOpState, error)
   227  
   228  	// AggregateTiles does large tile aggregation from source namespace to target namespace.
   229  	AggregateTiles(ctx context.Context, sourceNsID, targetNsID ident.ID, opts AggregateTilesOptions) (int64, error)
   230  }
   231  
   232  // database is the internal database interface.
   233  type database interface {
   234  	Database
   235  
   236  	// OwnedNamespaces returns the namespaces this database owns.
   237  	OwnedNamespaces() ([]databaseNamespace, error)
   238  
   239  	// UpdateOwnedNamespaces updates the namespaces this database owns.
   240  	UpdateOwnedNamespaces(namespaces namespace.Map) error
   241  }
   242  
   243  // Namespace is a time series database namespace.
   244  type Namespace interface {
   245  	// Options returns the namespace options.
   246  	Options() namespace.Options
   247  
   248  	// ID returns the ID of the namespace.
   249  	ID() ident.ID
   250  
   251  	// Metadata returns the metadata of the namespace.
   252  	Metadata() namespace.Metadata
   253  
   254  	// Schema returns the schema of the namespace.
   255  	Schema() namespace.SchemaDescr
   256  
   257  	// NumSeries returns the number of series in the namespace.
   258  	NumSeries() int64
   259  
   260  	// Shards returns the shard description.
   261  	Shards() []Shard
   262  
   263  	// ReadableShardAt returns a readable (bootstrapped) shard by id.
   264  	ReadableShardAt(shardID uint32) (databaseShard, namespace.Context, error)
   265  
   266  	// SetIndex sets and enables reverse index for this namespace.
   267  	SetIndex(reverseIndex NamespaceIndex) error
   268  
   269  	// Index returns the reverse index backing the namespace, if it exists.
   270  	Index() (NamespaceIndex, error)
   271  
   272  	// StorageOptions returns storage options.
   273  	StorageOptions() Options
   274  
   275  	// ReadOnly returns true if this Namespace is read only.
   276  	ReadOnly() bool
   277  
   278  	// SetReadOnly sets the value of ReadOnly option.
   279  	SetReadOnly(value bool)
   280  
   281  	// DocRef returns the doc if already present in a namespace shard.
   282  	DocRef(id ident.ID) (doc.Metadata, bool, error)
   283  }
   284  
   285  // NamespacesByID is a sortable slice of namespaces by ID.
   286  type NamespacesByID []Namespace
   287  
   288  func (n NamespacesByID) Len() int      { return len(n) }
   289  func (n NamespacesByID) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
   290  func (n NamespacesByID) Less(i, j int) bool {
   291  	return bytes.Compare(n[i].ID().Bytes(), n[j].ID().Bytes()) < 0
   292  }
   293  
   294  // SeriesWrite is a result of a series write.
   295  type SeriesWrite struct {
   296  	Series             ts.Series
   297  	WasWritten         bool
   298  	NeedsIndex         bool
   299  	PendingIndexInsert writes.PendingIndexInsert
   300  }
   301  
   302  type databaseNamespace interface {
   303  	Namespace
   304  
   305  	// Close will release the namespace resources and close the namespace.
   306  	Close() error
   307  
   308  	// AssignShardSet sets the shard set assignment and returns immediately.
   309  	AssignShardSet(shardSet sharding.ShardSet)
   310  
   311  	// OwnedShards returns the database shards.
   312  	OwnedShards() []databaseShard
   313  
   314  	// Tick performs any regular maintenance operations.
   315  	Tick(c context.Cancellable, startTime xtime.UnixNano) error
   316  
   317  	// Write writes a data point.
   318  	Write(
   319  		ctx context.Context,
   320  		id ident.ID,
   321  		timestamp xtime.UnixNano,
   322  		value float64,
   323  		unit xtime.Unit,
   324  		annotation []byte,
   325  	) (SeriesWrite, error)
   326  
   327  	// WriteTagged values to the namespace for an ID.
   328  	WriteTagged(
   329  		ctx context.Context,
   330  		id ident.ID,
   331  		tagResolver convert.TagMetadataResolver,
   332  		timestamp xtime.UnixNano,
   333  		value float64,
   334  		unit xtime.Unit,
   335  		annotation []byte,
   336  	) (SeriesWrite, error)
   337  
   338  	// QueryIDs resolves the given query into known IDs.
   339  	QueryIDs(
   340  		ctx context.Context,
   341  		query index.Query,
   342  		opts index.QueryOptions,
   343  	) (index.QueryResult, error)
   344  
   345  	// AggregateQuery resolves the given query into aggregated tags.
   346  	AggregateQuery(
   347  		ctx context.Context,
   348  		query index.Query,
   349  		opts index.AggregationOptions,
   350  	) (index.AggregateQueryResult, error)
   351  
   352  	// ReadEncoded reads data for given id within [start, end).
   353  	ReadEncoded(
   354  		ctx context.Context,
   355  		id ident.ID,
   356  		start, end xtime.UnixNano,
   357  	) (series.BlockReaderIter, error)
   358  
   359  	// FetchBlocks retrieves data blocks for a given id and a list of block
   360  	// start times.
   361  	FetchBlocks(
   362  		ctx context.Context,
   363  		shardID uint32,
   364  		id ident.ID,
   365  		starts []xtime.UnixNano,
   366  	) ([]block.FetchBlockResult, error)
   367  
   368  	// FetchBlocksMetadataV2 retrieves blocks metadata.
   369  	FetchBlocksMetadataV2(
   370  		ctx context.Context,
   371  		shardID uint32,
   372  		start, end xtime.UnixNano,
   373  		limit int64,
   374  		pageToken PageToken,
   375  		opts block.FetchBlocksMetadataOptions,
   376  	) (block.FetchBlocksMetadataResults, PageToken, error)
   377  
   378  	// PrepareBootstrap prepares the namespace for bootstrapping by ensuring
   379  	// it's shards know which flushed files reside on disk, so that calls
   380  	// to series.LoadBlock(...) will succeed.
   381  	PrepareBootstrap(ctx context.Context) ([]databaseShard, error)
   382  
   383  	// Bootstrap marks shards as bootstrapped for the namespace.
   384  	Bootstrap(ctx context.Context, bootstrapResult bootstrap.NamespaceResult) error
   385  
   386  	// WarmFlush flushes in-memory WarmWrites.
   387  	WarmFlush(blockStart xtime.UnixNano, flush persist.FlushPreparer) error
   388  
   389  	// FlushIndex flushes in-memory index data.
   390  	FlushIndex(flush persist.IndexFlush) error
   391  
   392  	// ColdFlush flushes unflushed in-memory ColdWrites.
   393  	ColdFlush(flush persist.FlushPreparer) error
   394  
   395  	// Snapshot snapshots unflushed in-memory warm and cold writes.
   396  	Snapshot(blockStarts []xtime.UnixNano, snapshotTime xtime.UnixNano, flush persist.SnapshotPreparer) error
   397  
   398  	// NeedsFlush returns true if the namespace needs a flush for the
   399  	// period: [start, end] (both inclusive).
   400  	// NB: The start/end times are assumed to be aligned to block size boundary.
   401  	NeedsFlush(alignedInclusiveStart xtime.UnixNano, alignedInclusiveEnd xtime.UnixNano) (bool, error)
   402  
   403  	// Truncate truncates the in-memory data for this namespace.
   404  	Truncate() (int64, error)
   405  
   406  	// Repair repairs the namespace data for a given time range.
   407  	Repair(repairer databaseShardRepairer, tr xtime.Range, opts NamespaceRepairOptions) error
   408  
   409  	// BootstrapState returns namespaces' bootstrap state.
   410  	BootstrapState() BootstrapState
   411  
   412  	// ShardBootstrapState captures and returns a snapshot of the namespaces' shards bootstrap state.
   413  	ShardBootstrapState() ShardBootstrapStates
   414  
   415  	// FlushState returns the flush state for the specified shard and block start.
   416  	FlushState(shardID uint32, blockStart xtime.UnixNano) (fileOpState, error)
   417  
   418  	// SeriesRefResolver returns a series ref resolver, callers
   419  	// must make sure to call the release callback once finished
   420  	// with the reference.
   421  	SeriesRefResolver(
   422  		shardID uint32,
   423  		id ident.ID,
   424  		tags ident.TagIterator,
   425  	) (result bootstrap.SeriesRefResolver, owned bool, err error)
   426  
   427  	// WritePendingIndexInserts will write any pending index inserts.
   428  	WritePendingIndexInserts(pending []writes.PendingIndexInsert) error
   429  
   430  	// AggregateTiles does large tile aggregation from source namespace into this namespace.
   431  	AggregateTiles(
   432  		ctx context.Context,
   433  		sourceNs databaseNamespace,
   434  		opts AggregateTilesOptions,
   435  	) (int64, error)
   436  }
   437  
   438  // NamespaceRepairOptions is a set of repair options for repairing a namespace.
   439  type NamespaceRepairOptions struct {
   440  	Force bool
   441  }
   442  
   443  // Shard is a time series database shard.
   444  type Shard interface {
   445  	// ID returns the ID of the shard.
   446  	ID() uint32
   447  
   448  	// NumSeries returns the number of series in the shard.
   449  	NumSeries() int64
   450  
   451  	// IsBootstrapped returns whether the shard is already bootstrapped.
   452  	IsBootstrapped() bool
   453  
   454  	// BootstrapState returns the shards' bootstrap state.
   455  	BootstrapState() BootstrapState
   456  
   457  	// OpenStreamingReader creates and opens a streaming fs.DataFileSetReader
   458  	// on the latest volume of the given block.
   459  	OpenStreamingReader(blockStart xtime.UnixNano) (fs.DataFileSetReader, error)
   460  
   461  	// TryRetrieveSeriesAndIncrementReaderWriterCount attempts to retrieve a writable series.
   462  	// This increments the reader/writer count and so should be decremented when the series
   463  	// is no longer held.
   464  	TryRetrieveSeriesAndIncrementReaderWriterCount(id ident.ID) (*Entry, WritableSeriesOptions, error)
   465  
   466  	// Close will release the shard resources and close the shard.
   467  	Close() error
   468  
   469  	// Closed indicates if shard was closed using Close.
   470  	Closed() bool
   471  }
   472  
   473  type databaseShard interface {
   474  	Shard
   475  
   476  	// OnEvictedFromWiredList is the same as block.Owner. Had to duplicate
   477  	// it here because mockgen chokes on embedded interfaces sometimes:
   478  	// https://github.com/golang/mock/issues/10
   479  	OnEvictedFromWiredList(id ident.ID, blockStart xtime.UnixNano)
   480  
   481  	// Tick performs all async updates
   482  	Tick(c context.Cancellable, startTime xtime.UnixNano, nsCtx namespace.Context) (tickResult, error)
   483  
   484  	// Write writes a value to the shard for an ID.
   485  	Write(
   486  		ctx context.Context,
   487  		id ident.ID,
   488  		timestamp xtime.UnixNano,
   489  		value float64,
   490  		unit xtime.Unit,
   491  		annotation []byte,
   492  		wOpts series.WriteOptions,
   493  	) (SeriesWrite, error)
   494  
   495  	// WriteTagged writes a value to the shard for an ID with tags.
   496  	WriteTagged(
   497  		ctx context.Context,
   498  		id ident.ID,
   499  		tagResolver convert.TagMetadataResolver,
   500  		timestamp xtime.UnixNano,
   501  		value float64,
   502  		unit xtime.Unit,
   503  		annotation []byte,
   504  		wOpts series.WriteOptions,
   505  	) (SeriesWrite, error)
   506  
   507  	ReadEncoded(
   508  		ctx context.Context,
   509  		id ident.ID,
   510  		start, end xtime.UnixNano,
   511  		nsCtx namespace.Context,
   512  	) (series.BlockReaderIter, error)
   513  
   514  	// FetchBlocks retrieves data blocks for a given id and a list of block
   515  	// start times.
   516  	FetchBlocks(
   517  		ctx context.Context,
   518  		id ident.ID,
   519  		starts []xtime.UnixNano,
   520  		nsCtx namespace.Context,
   521  	) ([]block.FetchBlockResult, error)
   522  
   523  	// FetchBlocksForColdFlush fetches blocks for a cold flush. This function
   524  	// informs the series and the buffer that a cold flush for the specified
   525  	// block start is occurring so that it knows to update bucket versions.
   526  	FetchBlocksForColdFlush(
   527  		ctx context.Context,
   528  		seriesID ident.ID,
   529  		start xtime.UnixNano,
   530  		version int,
   531  		nsCtx namespace.Context,
   532  	) (block.FetchBlockResult, error)
   533  
   534  	// FetchBlocksMetadataV2 retrieves blocks metadata.
   535  	FetchBlocksMetadataV2(
   536  		ctx context.Context,
   537  		start, end xtime.UnixNano,
   538  		limit int64,
   539  		pageToken PageToken,
   540  		opts block.FetchBlocksMetadataOptions,
   541  	) (block.FetchBlocksMetadataResults, PageToken, error)
   542  
   543  	// PrepareBootstrap prepares the shard for bootstrapping by ensuring
   544  	// it knows which flushed files reside on disk.
   545  	PrepareBootstrap(ctx context.Context) error
   546  
   547  	// Bootstrap bootstraps the shard after all provided data
   548  	// has been loaded using LoadBootstrapBlocks.
   549  	Bootstrap(ctx context.Context, nsCtx namespace.Context) error
   550  
   551  	// UpdateFlushStates updates all the flush states for the current shard
   552  	// by checking the file volumes that exist on disk at a point in time.
   553  	UpdateFlushStates()
   554  
   555  	// LoadBlocks does the same thing as LoadBootstrapBlocks,
   556  	// except it can be called more than once and after a shard is
   557  	// bootstrapped already.
   558  	LoadBlocks(series *result.Map) error
   559  
   560  	// WarmFlush flushes the WarmWrites in this shard.
   561  	WarmFlush(
   562  		blockStart xtime.UnixNano,
   563  		flush persist.FlushPreparer,
   564  		nsCtx namespace.Context,
   565  	) error
   566  
   567  	// MarkWarmIndexFlushStateSuccessOrError marks the blockStart as
   568  	// success or fail based on the provided err.
   569  	MarkWarmIndexFlushStateSuccessOrError(blockStart xtime.UnixNano, err error)
   570  
   571  	// ColdFlush flushes the unflushed ColdWrites in this shard.
   572  	ColdFlush(
   573  		flush persist.FlushPreparer,
   574  		resources coldFlushReusableResources,
   575  		nsCtx namespace.Context,
   576  		onFlush persist.OnFlushSeries,
   577  	) (ShardColdFlush, error)
   578  
   579  	// FilterBlocksNeedSnapshot computes which blocks require snapshots.
   580  	FilterBlocksNeedSnapshot(blockStarts []xtime.UnixNano) []xtime.UnixNano
   581  
   582  	// Snapshot snapshot's the unflushed WarmWrites in this shard.
   583  	Snapshot(
   584  		blockStart xtime.UnixNano,
   585  		snapshotStart xtime.UnixNano,
   586  		flush persist.SnapshotPreparer,
   587  		nsCtx namespace.Context,
   588  	) (ShardSnapshotResult, error)
   589  
   590  	// FlushState returns the flush state for this shard at block start.
   591  	FlushState(blockStart xtime.UnixNano) (fileOpState, error)
   592  
   593  	// CleanupExpiredFileSets removes expired fileset files.
   594  	CleanupExpiredFileSets(earliestToRetain xtime.UnixNano) error
   595  
   596  	// CleanupCompactedFileSets removes fileset files that have been compacted,
   597  	// meaning that there exists a more recent, superset, fully persisted
   598  	// fileset for that block.
   599  	CleanupCompactedFileSets() error
   600  
   601  	// Repair repairs the shard data for a given time.
   602  	Repair(
   603  		ctx context.Context,
   604  		nsCtx namespace.Context,
   605  		nsMeta namespace.Metadata,
   606  		tr xtime.Range,
   607  		repairer databaseShardRepairer,
   608  	) (repair.MetadataComparisonResult, error)
   609  
   610  	// SeriesRefResolver returns a series ref resolver, callers
   611  	// must make sure to call the release callback once finished
   612  	// with the reference.
   613  	SeriesRefResolver(
   614  		id ident.ID,
   615  		tags ident.TagIterator,
   616  	) (bootstrap.SeriesRefResolver, error)
   617  
   618  	// DocRef returns the doc if already present in a shard series.
   619  	DocRef(id ident.ID) (doc.Metadata, bool, error)
   620  
   621  	// AggregateTiles does large tile aggregation from source shards into this shard.
   622  	AggregateTiles(
   623  		ctx context.Context,
   624  		sourceNs Namespace,
   625  		targetNs Namespace,
   626  		shardID uint32,
   627  		onFlushSeries persist.OnFlushSeries,
   628  		opts AggregateTilesOptions,
   629  	) (int64, error)
   630  
   631  	// LatestVolume returns the latest volume for the combination of shard+blockStart.
   632  	LatestVolume(blockStart xtime.UnixNano) (int, error)
   633  }
   634  
   635  // ShardSnapshotResult is a result from a shard snapshot.
   636  type ShardSnapshotResult struct {
   637  	SeriesPersist int
   638  }
   639  
   640  // ShardColdFlush exposes a done method to finalize shard cold flush
   641  // by persisting data and updating shard state/block leases.
   642  type ShardColdFlush interface {
   643  	Done() error
   644  }
   645  
   646  // NamespaceIndex indexes namespace writes.
   647  type NamespaceIndex interface {
   648  	// AssignShardSet sets the shard set assignment and returns immediately.
   649  	AssignShardSet(shardSet sharding.ShardSet)
   650  
   651  	// BlockStartForWriteTime returns the index block start
   652  	// time for the given writeTime.
   653  	BlockStartForWriteTime(
   654  		writeTime xtime.UnixNano,
   655  	) xtime.UnixNano
   656  
   657  	// BlockForBlockStart returns an index block for a block start.
   658  	BlockForBlockStart(
   659  		blockStart xtime.UnixNano,
   660  	) (index.Block, error)
   661  
   662  	// WriteBatch indexes the provided entries.
   663  	WriteBatch(
   664  		batch *index.WriteBatch,
   665  	) error
   666  
   667  	// WritePending indexes the provided pending entries.
   668  	WritePending(
   669  		pending []writes.PendingIndexInsert,
   670  	) error
   671  
   672  	// Query resolves the given query into known IDs.
   673  	Query(
   674  		ctx context.Context,
   675  		query index.Query,
   676  		opts index.QueryOptions,
   677  	) (index.QueryResult, error)
   678  
   679  	// AggregateQuery resolves the given query into aggregated tags.
   680  	AggregateQuery(
   681  		ctx context.Context,
   682  		query index.Query,
   683  		opts index.AggregationOptions,
   684  	) (index.AggregateQueryResult, error)
   685  
   686  	// Bootstrap bootstraps the index with the provided segments.
   687  	Bootstrap(
   688  		bootstrapResults result.IndexResults,
   689  	) error
   690  
   691  	// Bootstrapped is true if the bootstrap has completed.
   692  	Bootstrapped() bool
   693  
   694  	// CleanupExpiredFileSets removes expired fileset files. Expiration is calcuated
   695  	// using the provided `t` as the frame of reference.
   696  	CleanupExpiredFileSets(t xtime.UnixNano) error
   697  
   698  	// CleanupCorruptedFileSets removes corrupted fileset files.
   699  	CleanupCorruptedFileSets() error
   700  
   701  	// CleanupDuplicateFileSets removes duplicate fileset files.
   702  	CleanupDuplicateFileSets(activeShards []uint32) error
   703  
   704  	// Tick performs internal house keeping in the index, including block rotation,
   705  	// data eviction, and so on.
   706  	Tick(c context.Cancellable, startTime xtime.UnixNano) (namespaceIndexTickResult, error)
   707  
   708  	// WarmFlush performs any warm flushes that the index has outstanding using
   709  	// the owned shards of the database.
   710  	WarmFlush(
   711  		flush persist.IndexFlush,
   712  		shards []databaseShard,
   713  	) error
   714  
   715  	// WarmFlushBlockStarts returns all index blockStarts which have been flushed to disk.
   716  	WarmFlushBlockStarts() []xtime.UnixNano
   717  
   718  	// ColdFlush performs any cold flushes that the index has outstanding using
   719  	// the owned shards of the database. Also returns a callback to be called when
   720  	// cold flushing completes to perform houskeeping.
   721  	ColdFlush(shards []databaseShard) (OnColdFlushDone, error)
   722  
   723  	// DebugMemorySegments allows for debugging memory segments.
   724  	DebugMemorySegments(opts DebugMemorySegmentsOptions) error
   725  
   726  	// BackgroundCompact background compacts eligible segments.
   727  	BackgroundCompact()
   728  
   729  	// Close will release the index resources and close the index.
   730  	Close() error
   731  }
   732  
   733  // OnColdFlushDone is a callback that performs house keeping once cold flushing completes.
   734  type OnColdFlushDone func() error
   735  
   736  // DebugMemorySegmentsOptions is a set of options to debug memory segments.
   737  type DebugMemorySegmentsOptions struct {
   738  	OutputDirectory string
   739  }
   740  
   741  // namespaceIndexTickResult are details about the work performed by the namespaceIndex
   742  // during a Tick().
   743  type namespaceIndexTickResult struct {
   744  	NumBlocks               int64
   745  	NumBlocksSealed         int64
   746  	NumBlocksEvicted        int64
   747  	NumSegments             int64
   748  	NumSegmentsBootstrapped int64
   749  	NumSegmentsMutable      int64
   750  	NumTotalDocs            int64
   751  	FreeMmap                int64
   752  }
   753  
   754  // namespaceIndexInsertQueue is a queue used in-front of the indexing component
   755  // for Writes. NB: this is an interface to allow easier unit tests in namespaceIndex.
   756  type namespaceIndexInsertQueue interface {
   757  	// Start starts accepting writes in the queue.
   758  	Start() error
   759  
   760  	// Stop stops accepting writes in the queue.
   761  	Stop() error
   762  
   763  	// InsertBatch inserts the provided documents to the index queue which processes
   764  	// inserts to the index asynchronously. It executes the provided callbacks
   765  	// based on the result of the execution. The returned wait group can be used
   766  	// if the insert is required to be synchronous.
   767  	InsertBatch(
   768  		batch *index.WriteBatch,
   769  	) (*sync.WaitGroup, error)
   770  
   771  	// InsertPending inserts the provided documents to the index queue which processes
   772  	// inserts to the index asynchronously. It executes the provided callbacks
   773  	// based on the result of the execution. The returned wait group can be used
   774  	// if the insert is required to be synchronous.
   775  	InsertPending(
   776  		pending []writes.PendingIndexInsert,
   777  	) (*sync.WaitGroup, error)
   778  }
   779  
   780  // databaseBootstrapManager manages the bootstrap process.
   781  type databaseBootstrapManager interface {
   782  	// IsBootstrapped returns whether the database is already bootstrapped.
   783  	IsBootstrapped() bool
   784  
   785  	// LastBootstrapCompletionTime returns the last bootstrap completion time,
   786  	// if any.
   787  	LastBootstrapCompletionTime() (xtime.UnixNano, bool)
   788  
   789  	// Bootstrap performs bootstrapping for all namespaces and shards owned.
   790  	Bootstrap() (BootstrapResult, error)
   791  
   792  	// BootstrapEnqueue performs bootstrapping asynchronously for all namespaces and shards owned.
   793  	BootstrapEnqueue(opts BootstrapEnqueueOptions)
   794  
   795  	// Report reports runtime information.
   796  	Report()
   797  }
   798  
   799  // BootstrapCompleteFn is a callback for when bootstrap is complete when using
   800  // BootstrapEnqueue method.
   801  type BootstrapCompleteFn func(BootstrapResult)
   802  
   803  // BootstrapEnqueueOptions is options to pass to BootstrapEnqueue when
   804  // enqueuing a bootstrap.
   805  type BootstrapEnqueueOptions struct {
   806  	// OnCompleteFn is an optional function to pass to execute once
   807  	// the set of queued bootstraps are complete.
   808  	OnCompleteFn BootstrapCompleteFn
   809  }
   810  
   811  // BootstrapResult is a bootstrap result.
   812  type BootstrapResult struct {
   813  	ErrorsBootstrap      []error
   814  	AlreadyBootstrapping bool
   815  }
   816  
   817  // databaseFlushManager manages flushing in-memory data to persistent storage.
   818  type databaseFlushManager interface {
   819  	// Flush flushes in-memory data to persistent storage.
   820  	Flush(startTime xtime.UnixNano) error
   821  
   822  	// LastSuccessfulSnapshotStartTime returns the start time of the last
   823  	// successful snapshot, if any.
   824  	LastSuccessfulSnapshotStartTime() (xtime.UnixNano, bool)
   825  
   826  	// Report reports runtime information.
   827  	Report()
   828  }
   829  
   830  // databaseCleanupManager manages cleaning up persistent storage space.
   831  // NB(bodu): We have to separate flush methods since we separated out flushing into warm/cold flush
   832  // and cleaning up certain types of data concurrently w/ either can be problematic.
   833  type databaseCleanupManager interface {
   834  	// WarmFlushCleanup cleans up data not needed in the persistent storage before a warm flush.
   835  	WarmFlushCleanup(t xtime.UnixNano) error
   836  
   837  	// ColdFlushCleanup cleans up data not needed in the persistent storage before a cold flush.
   838  	ColdFlushCleanup(t xtime.UnixNano) error
   839  
   840  	// Report reports runtime information.
   841  	Report()
   842  }
   843  
   844  // databaseFileSystemManager manages the database related filesystem activities.
   845  type databaseFileSystemManager interface {
   846  	// Flush flushes in-memory data to persistent storage.
   847  	Flush(t xtime.UnixNano) error
   848  
   849  	// Disable disables the filesystem manager and prevents it from
   850  	// performing file operations, returns the current file operation status.
   851  	Disable() fileOpStatus
   852  
   853  	// Enable enables the filesystem manager to perform file operations.
   854  	Enable() fileOpStatus
   855  
   856  	// Status returns the file operation status.
   857  	Status() fileOpStatus
   858  
   859  	// Run attempts to perform all filesystem-related operations,
   860  	// returning true if those operations are performed, and false otherwise.
   861  	Run(t xtime.UnixNano) bool
   862  
   863  	// Report reports runtime information.
   864  	Report()
   865  
   866  	// LastSuccessfulSnapshotStartTime returns the start time of the last
   867  	// successful snapshot, if any.
   868  	LastSuccessfulSnapshotStartTime() (xtime.UnixNano, bool)
   869  }
   870  
   871  // databaseColdFlushManager manages the database related cold flush activities.
   872  type databaseColdFlushManager interface {
   873  	databaseCleanupManager
   874  
   875  	// Disable disables the cold flush manager and prevents it from
   876  	// performing file operations, returns the current file operation status.
   877  	Disable() fileOpStatus
   878  
   879  	// Enable enables the cold flush manager to perform file operations.
   880  	Enable() fileOpStatus
   881  
   882  	// Status returns the file operation status.
   883  	Status() fileOpStatus
   884  
   885  	// Run attempts to perform all cold flush related operations,
   886  	// returning true if those operations are performed, and false otherwise.
   887  	Run(t xtime.UnixNano) bool
   888  }
   889  
   890  // databaseShardRepairer repairs in-memory data for a shard.
   891  type databaseShardRepairer interface {
   892  	// Options returns the repair options.
   893  	Options() repair.Options
   894  
   895  	// Repair repairs the data for a given namespace and shard.
   896  	Repair(
   897  		ctx context.Context,
   898  		nsCtx namespace.Context,
   899  		nsMeta namespace.Metadata,
   900  		tr xtime.Range,
   901  		shard databaseShard,
   902  	) (repair.MetadataComparisonResult, error)
   903  }
   904  
   905  // BackgroundProcess is a background process that is run by the database.
   906  type BackgroundProcess interface {
   907  	// Start launches the BackgroundProcess to run asynchronously.
   908  	Start()
   909  
   910  	// Stop stops the BackgroundProcess.
   911  	Stop()
   912  
   913  	// Report reports runtime information.
   914  	Report()
   915  }
   916  
   917  // FileOpsProcess is a background process that is run by the database.
   918  type FileOpsProcess interface {
   919  	// Start launches the FileOpsProcess to run asynchronously.
   920  	Start()
   921  }
   922  
   923  // FileOpsProcessFn is a file ops process function.
   924  type FileOpsProcessFn func()
   925  
   926  // Start starts file ops process function.
   927  func (f FileOpsProcessFn) Start() {
   928  	// delegate to the anonymous function.
   929  	f()
   930  }
   931  
   932  // databaseRepairer repairs in-memory database data.
   933  type databaseRepairer interface {
   934  	BackgroundProcess
   935  
   936  	// Repair repairs in-memory data.
   937  	Repair() error
   938  }
   939  
   940  // databaseTickManager performs periodic ticking.
   941  type databaseTickManager interface {
   942  	// Tick performs maintenance operations, restarting the current
   943  	// tick if force is true. It returns nil if a new tick has
   944  	// completed successfully, and an error otherwise.
   945  	Tick(forceType forceType, startTime xtime.UnixNano) error
   946  }
   947  
   948  // databaseMediator mediates actions among various database managers.
   949  type databaseMediator interface {
   950  	// Open opens the mediator.
   951  	Open() error
   952  
   953  	// RegisterBackgroundProcess registers a BackgroundProcess to be executed by the mediator. Must happen before Open().
   954  	RegisterBackgroundProcess(process BackgroundProcess) error
   955  
   956  	// IsBootstrapped returns whether the database is bootstrapped.
   957  	IsBootstrapped() bool
   958  
   959  	// IsOpen returns whether mediator is opened.
   960  	IsOpen() bool
   961  
   962  	// LastBootstrapCompletionTime returns the last bootstrap completion time,
   963  	// if any.
   964  	LastBootstrapCompletionTime() (xtime.UnixNano, bool)
   965  
   966  	// Bootstrap bootstraps the database with file operations performed at the end.
   967  	Bootstrap() (BootstrapResult, error)
   968  
   969  	// BootstrapEnqueue performs bootstrapping asynchronously for all namespaces and shards owned.
   970  	BootstrapEnqueue(opts BootstrapEnqueueOptions)
   971  
   972  	// DisableFileOpsAndWait disables file operations.
   973  	DisableFileOpsAndWait()
   974  
   975  	// EnableFileOps enables file operations.
   976  	EnableFileOps()
   977  
   978  	// Tick performs a tick.
   979  	Tick(forceType forceType, startTime xtime.UnixNano) error
   980  
   981  	// Close closes the mediator.
   982  	Close() error
   983  
   984  	// Report reports runtime information.
   985  	Report()
   986  
   987  	// LastSuccessfulSnapshotStartTime returns the start time of the last
   988  	// successful snapshot, if any.
   989  	LastSuccessfulSnapshotStartTime() (xtime.UnixNano, bool)
   990  }
   991  
   992  // ColdFlushNsOpts are options for OnColdFlush.ColdFlushNamespace.
   993  type ColdFlushNsOpts interface {
   994  	// ReuseResources enables reuse of per-namespace reusable resources.
   995  	ReuseResources() bool
   996  }
   997  
   998  // OnColdFlush can perform work each time a series is flushed.
   999  type OnColdFlush interface {
  1000  	ColdFlushNamespace(ns Namespace, opts ColdFlushNsOpts) (OnColdFlushNamespace, error)
  1001  }
  1002  
  1003  // OnColdFlushNamespace performs work on a per namespace level.
  1004  type OnColdFlushNamespace interface {
  1005  	persist.OnFlushSeries
  1006  	Abort() error
  1007  	Done() error
  1008  }
  1009  
  1010  // OptionTransform transforms given Options.
  1011  type OptionTransform func(Options) Options
  1012  
  1013  // Options represents the options for storage.
  1014  type Options interface {
  1015  	// Validate validates assumptions baked into the code.
  1016  	Validate() error
  1017  
  1018  	// SetEncodingM3TSZPooled sets m3tsz encoding with pooling.
  1019  	SetEncodingM3TSZPooled() Options
  1020  
  1021  	// SetClockOptions sets the clock options.
  1022  	SetClockOptions(value clock.Options) Options
  1023  
  1024  	// ClockOptions returns the clock options.
  1025  	ClockOptions() clock.Options
  1026  
  1027  	// SetInstrumentOptions sets the instrumentation options.
  1028  	SetInstrumentOptions(value instrument.Options) Options
  1029  
  1030  	// InstrumentOptions returns the instrumentation options.
  1031  	InstrumentOptions() instrument.Options
  1032  
  1033  	// SetNamespaceInitializer sets the namespace registry initializer.
  1034  	SetNamespaceInitializer(value namespace.Initializer) Options
  1035  
  1036  	// NamespaceInitializer returns the namespace registry initializer.
  1037  	NamespaceInitializer() namespace.Initializer
  1038  
  1039  	// SetDatabaseBlockOptions sets the database block options.
  1040  	SetDatabaseBlockOptions(value block.Options) Options
  1041  
  1042  	// DatabaseBlockOptions returns the database block options.
  1043  	DatabaseBlockOptions() block.Options
  1044  
  1045  	// SetCommitLogOptions sets the commit log options.
  1046  	SetCommitLogOptions(value commitlog.Options) Options
  1047  
  1048  	// CommitLogOptions returns the commit log options.
  1049  	CommitLogOptions() commitlog.Options
  1050  
  1051  	// SetRuntimeOptionsManager sets the runtime options manager.
  1052  	SetRuntimeOptionsManager(value runtime.OptionsManager) Options
  1053  
  1054  	// RuntimeOptionsManager returns the runtime options manager.
  1055  	RuntimeOptionsManager() runtime.OptionsManager
  1056  
  1057  	// SetErrorWindowForLoad sets the error window for load.
  1058  	SetErrorWindowForLoad(value time.Duration) Options
  1059  
  1060  	// ErrorWindowForLoad returns the error window for load.
  1061  	ErrorWindowForLoad() time.Duration
  1062  
  1063  	// SetErrorThresholdForLoad sets the error threshold for load.
  1064  	SetErrorThresholdForLoad(value int64) Options
  1065  
  1066  	// ErrorThresholdForLoad returns the error threshold for load.
  1067  	ErrorThresholdForLoad() int64
  1068  
  1069  	// SetIndexOptions set the indexing options.
  1070  	SetIndexOptions(value index.Options) Options
  1071  
  1072  	// IndexOptions returns the indexing options.
  1073  	IndexOptions() index.Options
  1074  
  1075  	// SetTruncateType sets the truncation type for the database.
  1076  	SetTruncateType(value series.TruncateType) Options
  1077  
  1078  	// TruncateType returns the truncation type for the database.
  1079  	TruncateType() series.TruncateType
  1080  
  1081  	// SetWriteTransformOptions sets options for transforming incoming writes
  1082  	// to the database.
  1083  	SetWriteTransformOptions(value series.WriteTransformOptions) Options
  1084  
  1085  	// WriteTransformOptions returns the options for transforming incoming writes
  1086  	// to the database.
  1087  	WriteTransformOptions() series.WriteTransformOptions
  1088  
  1089  	// SetRepairEnabled sets whether or not to enable the repair.
  1090  	SetRepairEnabled(b bool) Options
  1091  
  1092  	// RepairEnabled returns whether the repair is enabled.
  1093  	RepairEnabled() bool
  1094  
  1095  	// SetRepairOptions sets the repair options.
  1096  	SetRepairOptions(value repair.Options) Options
  1097  
  1098  	// RepairOptions returns the repair options.
  1099  	RepairOptions() repair.Options
  1100  
  1101  	// SetBootstrapProcessProvider sets the bootstrap process provider for the database.
  1102  	SetBootstrapProcessProvider(value bootstrap.ProcessProvider) Options
  1103  
  1104  	// BootstrapProcessProvider returns the bootstrap process provider for the database.
  1105  	BootstrapProcessProvider() bootstrap.ProcessProvider
  1106  
  1107  	// SetPersistManager sets the persistence manager.
  1108  	SetPersistManager(value persist.Manager) Options
  1109  
  1110  	// PersistManager returns the persistence manager.
  1111  	PersistManager() persist.Manager
  1112  
  1113  	// SetIndexClaimsManager sets the index claims manager.
  1114  	SetIndexClaimsManager(value fs.IndexClaimsManager) Options
  1115  
  1116  	// IndexClaimsManager returns the index claims manager.
  1117  	IndexClaimsManager() fs.IndexClaimsManager
  1118  
  1119  	// SetDatabaseBlockRetrieverManager sets the block retriever manager to
  1120  	// use when bootstrapping retrievable blocks instead of blocks
  1121  	// containing data.
  1122  	// If you don't wish to bootstrap retrievable blocks instead of
  1123  	// blocks containing data then do not set this manager.
  1124  	// You can opt into which namespace you wish to have this enabled for
  1125  	// by returning nil instead of a result when creating a new block retriever
  1126  	// for a namespace from the manager.
  1127  	SetDatabaseBlockRetrieverManager(
  1128  		value block.DatabaseBlockRetrieverManager,
  1129  	) Options
  1130  
  1131  	// NewBlockRetrieverFn returns the new block retriever constructor to
  1132  	// use when bootstrapping retrievable blocks instead of blocks
  1133  	// containing data.
  1134  	DatabaseBlockRetrieverManager() block.DatabaseBlockRetrieverManager
  1135  
  1136  	// SetContextPool sets the contextPool.
  1137  	SetContextPool(value context.Pool) Options
  1138  
  1139  	// ContextPool returns the contextPool.
  1140  	ContextPool() context.Pool
  1141  
  1142  	// SetSeriesCachePolicy sets the series cache policy.
  1143  	SetSeriesCachePolicy(value series.CachePolicy) Options
  1144  
  1145  	// SeriesCachePolicy returns the series cache policy.
  1146  	SeriesCachePolicy() series.CachePolicy
  1147  
  1148  	// SetSeriesOptions sets the series options.
  1149  	SetSeriesOptions(value series.Options) Options
  1150  
  1151  	// SeriesOptions returns the series options.
  1152  	SeriesOptions() series.Options
  1153  
  1154  	// SetDatabaseSeriesPool sets the database series pool.
  1155  	SetDatabaseSeriesPool(value series.DatabaseSeriesPool) Options
  1156  
  1157  	// DatabaseSeriesPool returns the database series pool.
  1158  	DatabaseSeriesPool() series.DatabaseSeriesPool
  1159  
  1160  	// SetBytesPool sets the bytesPool.
  1161  	SetBytesPool(value pool.CheckedBytesPool) Options
  1162  
  1163  	// BytesPool returns the bytesPool.
  1164  	BytesPool() pool.CheckedBytesPool
  1165  
  1166  	// SetEncoderPool sets the contextPool.
  1167  	SetEncoderPool(value encoding.EncoderPool) Options
  1168  
  1169  	// EncoderPool returns the contextPool.
  1170  	EncoderPool() encoding.EncoderPool
  1171  
  1172  	// SetSegmentReaderPool sets the contextPool.
  1173  	SetSegmentReaderPool(value xio.SegmentReaderPool) Options
  1174  
  1175  	// SegmentReaderPool returns the contextPool.
  1176  	SegmentReaderPool() xio.SegmentReaderPool
  1177  
  1178  	// SetReaderIteratorPool sets the readerIteratorPool.
  1179  	SetReaderIteratorPool(value encoding.ReaderIteratorPool) Options
  1180  
  1181  	// ReaderIteratorPool returns the readerIteratorPool.
  1182  	ReaderIteratorPool() encoding.ReaderIteratorPool
  1183  
  1184  	// SetMultiReaderIteratorPool sets the multiReaderIteratorPool.
  1185  	SetMultiReaderIteratorPool(value encoding.MultiReaderIteratorPool) Options
  1186  
  1187  	// MultiReaderIteratorPool returns the multiReaderIteratorPool.
  1188  	MultiReaderIteratorPool() encoding.MultiReaderIteratorPool
  1189  
  1190  	// SetIdentifierPool sets the ID pool.
  1191  	SetIdentifierPool(value ident.Pool) Options
  1192  
  1193  	// IdentifierPool returns the ID pool.
  1194  	IdentifierPool() ident.Pool
  1195  
  1196  	// SetFetchBlockMetadataResultsPool sets the fetchBlockMetadataResultsPool.
  1197  	SetFetchBlockMetadataResultsPool(value block.FetchBlockMetadataResultsPool) Options
  1198  
  1199  	// FetchBlockMetadataResultsPool returns the fetchBlockMetadataResultsPool.
  1200  	FetchBlockMetadataResultsPool() block.FetchBlockMetadataResultsPool
  1201  
  1202  	// SetFetchBlocksMetadataResultsPool sets the fetchBlocksMetadataResultsPool.
  1203  	SetFetchBlocksMetadataResultsPool(value block.FetchBlocksMetadataResultsPool) Options
  1204  
  1205  	// FetchBlocksMetadataResultsPool returns the fetchBlocksMetadataResultsPool.
  1206  	FetchBlocksMetadataResultsPool() block.FetchBlocksMetadataResultsPool
  1207  
  1208  	// SetWriteBatchPool sets the WriteBatch pool.
  1209  	SetWriteBatchPool(value *writes.WriteBatchPool) Options
  1210  
  1211  	// WriteBatchPool returns the WriteBatch pool.
  1212  	WriteBatchPool() *writes.WriteBatchPool
  1213  
  1214  	// SetBufferBucketPool sets the BufferBucket pool.
  1215  	SetBufferBucketPool(value *series.BufferBucketPool) Options
  1216  
  1217  	// BufferBucketPool returns the BufferBucket pool.
  1218  	BufferBucketPool() *series.BufferBucketPool
  1219  
  1220  	// SetBufferBucketVersionsPool sets the BufferBucketVersions pool.
  1221  	SetBufferBucketVersionsPool(value *series.BufferBucketVersionsPool) Options
  1222  
  1223  	// BufferBucketVersionsPool returns the BufferBucketVersions pool.
  1224  	BufferBucketVersionsPool() *series.BufferBucketVersionsPool
  1225  
  1226  	// SetRetrieveRequestPool sets the retrieve request pool.
  1227  	SetRetrieveRequestPool(value fs.RetrieveRequestPool) Options
  1228  
  1229  	// RetrieveRequestPool gets the retrieve request pool.
  1230  	RetrieveRequestPool() fs.RetrieveRequestPool
  1231  
  1232  	// SetCheckedBytesWrapperPool sets the checked bytes wrapper pool.
  1233  	SetCheckedBytesWrapperPool(value xpool.CheckedBytesWrapperPool) Options
  1234  
  1235  	// CheckedBytesWrapperPool returns the checked bytes wrapper pool.
  1236  	CheckedBytesWrapperPool() xpool.CheckedBytesWrapperPool
  1237  
  1238  	// SetSchemaRegistry sets the schema registry the database uses.
  1239  	SetSchemaRegistry(registry namespace.SchemaRegistry) Options
  1240  
  1241  	// SchemaRegistry returns the schema registry the database uses.
  1242  	SchemaRegistry() namespace.SchemaRegistry
  1243  
  1244  	// SetBlockLeaseManager sets the block leaser.
  1245  	SetBlockLeaseManager(leaseMgr block.LeaseManager) Options
  1246  
  1247  	// BlockLeaseManager returns the block leaser.
  1248  	BlockLeaseManager() block.LeaseManager
  1249  
  1250  	// SetOnColdFlush sets the on cold flush processor.
  1251  	SetOnColdFlush(value OnColdFlush) Options
  1252  
  1253  	// OnColdFlush returns the on cold flush processor.
  1254  	OnColdFlush() OnColdFlush
  1255  
  1256  	// SetIterationOptions sets iteration options.
  1257  	SetIterationOptions(index.IterationOptions) Options
  1258  
  1259  	// IterationOptions returns iteration options.
  1260  	IterationOptions() index.IterationOptions
  1261  
  1262  	// SetForceColdWritesEnabled sets options for forcing cold writes.
  1263  	SetForceColdWritesEnabled(value bool) Options
  1264  
  1265  	// ForceColdWritesEnabled returns options for forcing cold writes.
  1266  	ForceColdWritesEnabled() bool
  1267  
  1268  	// SetSourceLoggerBuilder sets the limit source logger builder.
  1269  	SetSourceLoggerBuilder(value limits.SourceLoggerBuilder) Options
  1270  
  1271  	// SourceLoggerBuilder returns the limit source logger builder.
  1272  	SourceLoggerBuilder() limits.SourceLoggerBuilder
  1273  
  1274  	// SetMemoryTracker sets the MemoryTracker.
  1275  	SetMemoryTracker(memTracker MemoryTracker) Options
  1276  
  1277  	// MemoryTracker returns the MemoryTracker.
  1278  	MemoryTracker() MemoryTracker
  1279  
  1280  	// SetMmapReporter sets the mmap reporter.
  1281  	SetMmapReporter(mmapReporter mmap.Reporter) Options
  1282  
  1283  	// MmapReporter returns the mmap reporter.
  1284  	MmapReporter() mmap.Reporter
  1285  
  1286  	// SetDoNotIndexWithFieldsMap sets a map which if fields match it
  1287  	// will not index those metrics.
  1288  	SetDoNotIndexWithFieldsMap(value map[string]string) Options
  1289  
  1290  	// DoNotIndexWithFieldsMap returns a map which if fields match it
  1291  	// will not index those metrics.
  1292  	DoNotIndexWithFieldsMap() map[string]string
  1293  
  1294  	// SetNamespaceRuntimeOptionsManagerRegistry sets the namespace runtime options manager.
  1295  	SetNamespaceRuntimeOptionsManagerRegistry(value namespace.RuntimeOptionsManagerRegistry) Options
  1296  
  1297  	// NamespaceRuntimeOptionsManagerRegistry returns the namespace runtime options manager.
  1298  	NamespaceRuntimeOptionsManagerRegistry() namespace.RuntimeOptionsManagerRegistry
  1299  
  1300  	// SetMediatorTickInterval sets the ticking interval for the mediator.
  1301  	SetMediatorTickInterval(value time.Duration) Options
  1302  
  1303  	// MediatorTickInterval returns the ticking interval for the mediator.
  1304  	MediatorTickInterval() time.Duration
  1305  
  1306  	// SetAdminClient sets the admin client for the database options.
  1307  	SetAdminClient(value client.AdminClient) Options
  1308  
  1309  	// AdminClient returns the admin client.
  1310  	AdminClient() client.AdminClient
  1311  
  1312  	// SetBackgroundProcessFns sets the list of functions that create background processes for the database.
  1313  	SetBackgroundProcessFns([]NewBackgroundProcessFn) Options
  1314  
  1315  	// BackgroundProcessFns returns the list of functions that create background processes for the database.
  1316  	BackgroundProcessFns() []NewBackgroundProcessFn
  1317  
  1318  	// SetNamespaceHooks sets the NamespaceHooks.
  1319  	SetNamespaceHooks(hooks NamespaceHooks) Options
  1320  
  1321  	// NamespaceHooks returns the NamespaceHooks.
  1322  	NamespaceHooks() NamespaceHooks
  1323  
  1324  	// SetTileAggregator sets the TileAggregator.
  1325  	SetTileAggregator(aggregator TileAggregator) Options
  1326  
  1327  	// TileAggregator returns the TileAggregator.
  1328  	TileAggregator() TileAggregator
  1329  
  1330  	// PermitsOptions returns the permits options.
  1331  	PermitsOptions() permits.Options
  1332  
  1333  	// SetPermitsOptions sets the permits options.
  1334  	SetPermitsOptions(value permits.Options) Options
  1335  
  1336  	// LimitsOptions returns the limit options.
  1337  	LimitsOptions() limits.Options
  1338  
  1339  	// SetLimitsOptions sets the limits options.
  1340  	SetLimitsOptions(value limits.Options) Options
  1341  
  1342  	// CoreFn gets the function for determining the current core.
  1343  	CoreFn() xsync.CoreFn
  1344  
  1345  	// SetCoreFn sets the function for determining the current core.
  1346  	SetCoreFn(value xsync.CoreFn) Options
  1347  }
  1348  
  1349  // MemoryTracker tracks memory.
  1350  type MemoryTracker interface {
  1351  	// IncNumLoadedBytes increments the number of bytes that have been loaded
  1352  	// into memory via the "Load()" API.
  1353  	IncNumLoadedBytes(x int64) (okToLoad bool)
  1354  
  1355  	// NumLoadedBytes returns the number of bytes that have been loaded into memory via the
  1356  	// "Load()" API.
  1357  	NumLoadedBytes() int64
  1358  
  1359  	// MarkLoadedAsPending marks the current number of loaded bytes as pending
  1360  	// so that a subsequent call to DecPendingLoadedBytes() will decrement the
  1361  	// number of loaded bytes by the number that was set when this function was
  1362  	// last executed.
  1363  	MarkLoadedAsPending()
  1364  
  1365  	// DecPendingLoadedBytes decrements the number of loaded bytes by the number
  1366  	// of pending bytes that were captured by the last call to MarkLoadedAsPending().
  1367  	DecPendingLoadedBytes()
  1368  
  1369  	// WaitForDec waits for the next call to DecPendingLoadedBytes before returning.
  1370  	WaitForDec()
  1371  }
  1372  
  1373  // DatabaseBootstrapState stores a snapshot of the bootstrap state for all shards across all
  1374  // namespaces at a given moment in time.
  1375  type DatabaseBootstrapState struct {
  1376  	NamespaceBootstrapStates NamespaceBootstrapStates
  1377  }
  1378  
  1379  // NamespaceBootstrapStates stores a snapshot of the bootstrap state for all shards across a
  1380  // number of namespaces at a given moment in time.
  1381  type NamespaceBootstrapStates map[string]ShardBootstrapStates
  1382  
  1383  // ShardBootstrapStates stores a snapshot of the bootstrap state for all shards for a given
  1384  // namespace.
  1385  type ShardBootstrapStates map[uint32]BootstrapState
  1386  
  1387  // BootstrapState is an enum representing the possible bootstrap states for a shard.
  1388  type BootstrapState int
  1389  
  1390  const (
  1391  	// BootstrapNotStarted indicates bootstrap has not been started yet.
  1392  	BootstrapNotStarted BootstrapState = iota
  1393  	// Bootstrapping indicates bootstrap process is in progress.
  1394  	Bootstrapping
  1395  	// Bootstrapped indicates a bootstrap process has completed.
  1396  	Bootstrapped
  1397  )
  1398  
  1399  type newFSMergeWithMemFn func(
  1400  	shard databaseShard,
  1401  	retriever series.QueryableBlockRetriever,
  1402  	dirtySeries *dirtySeriesMap,
  1403  	dirtySeriesToWrite map[xtime.UnixNano]*idList,
  1404  ) fs.MergeWith
  1405  
  1406  // NewBackgroundProcessFn is a function that creates and returns a new BackgroundProcess.
  1407  type NewBackgroundProcessFn func(Database, Options) (BackgroundProcess, error)
  1408  
  1409  // AggregateTilesProcess identifies the process used for the aggregation.
  1410  type AggregateTilesProcess uint8
  1411  
  1412  const (
  1413  	// AggregateTilesRegular indicates regular process.
  1414  	AggregateTilesRegular AggregateTilesProcess = iota
  1415  
  1416  	// AggregateTilesBackfill indicates backfill.
  1417  	AggregateTilesBackfill
  1418  
  1419  	// AggregateTilesAPI indicates invocation via API call.
  1420  	AggregateTilesAPI
  1421  )
  1422  
  1423  // AggregateTilesProcesses is a list of available AggregateTilesProcess values.
  1424  var AggregateTilesProcesses = []AggregateTilesProcess{
  1425  	AggregateTilesRegular,
  1426  	AggregateTilesBackfill,
  1427  	AggregateTilesAPI,
  1428  }
  1429  
  1430  func (p AggregateTilesProcess) String() string {
  1431  	switch p {
  1432  	case AggregateTilesRegular:
  1433  		return "regular"
  1434  	case AggregateTilesBackfill:
  1435  		return "backfill"
  1436  	case AggregateTilesAPI:
  1437  		return "api"
  1438  	default:
  1439  		return fmt.Sprintf("unknown (%d)", p)
  1440  	}
  1441  }
  1442  
  1443  // AggregateTilesOptions is the options for large tile aggregation.
  1444  type AggregateTilesOptions struct {
  1445  	// Start and End specify the aggregation window.
  1446  	Start, End xtime.UnixNano
  1447  	// Step is the downsampling step.
  1448  	Step       time.Duration
  1449  	InsOptions instrument.Options
  1450  	Process    AggregateTilesProcess
  1451  
  1452  	// MemorizeMetricTypes enables storing data into MetricTypeByName map.
  1453  	MemorizeMetricTypes bool
  1454  	// MemorizeMetricTypes enables assigning metric types from MetricTypeByName map.
  1455  	BackfillMetricTypes bool
  1456  
  1457  	// MetricTypeByName is used when either MemorizeMetricTypes or BackfillMetricTypes is true.
  1458  	MetricTypeByName map[string]annotation.Payload
  1459  }
  1460  
  1461  // TileAggregator is the interface for AggregateTiles.
  1462  type TileAggregator interface {
  1463  	// AggregateTiles does tile aggregation.
  1464  	AggregateTiles(
  1465  		ctx context.Context,
  1466  		sourceNs, targetNs Namespace,
  1467  		shardID uint32,
  1468  		onFlushSeries persist.OnFlushSeries,
  1469  		opts AggregateTilesOptions,
  1470  	) (processedTileCount int64, nextVolume int, err error)
  1471  }
  1472  
  1473  // NewTileAggregatorFn creates a new TileAggregator.
  1474  type NewTileAggregatorFn func(iOpts instrument.Options) TileAggregator
  1475  
  1476  // NamespaceHooks allows dynamic plugging into the namespace lifecycle.
  1477  type NamespaceHooks interface {
  1478  	// OnCreatedNamespace gets invoked after each namespace is initialized.
  1479  	OnCreatedNamespace(Namespace, GetNamespaceFn) error
  1480  }
  1481  
  1482  // GetNamespaceFn will return a namespace for a given ID if present.
  1483  type GetNamespaceFn func(id ident.ID) (Namespace, bool)