github.com/m3db/m3@v1.5.0/src/query/graphite/storage/storage.go (about)

     1  // Copyright (c) 2019 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  	stdcontext "context"
    25  	"sync"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/query/block"
    29  	"github.com/m3db/m3/src/query/graphite/context"
    30  	"github.com/m3db/m3/src/query/graphite/ts"
    31  	querystorage "github.com/m3db/m3/src/query/storage"
    32  	"github.com/m3db/m3/src/query/storage/m3/consolidators"
    33  )
    34  
    35  // FetchOptions provides context to a fetch expression.
    36  type FetchOptions struct {
    37  	// StartTime is the start time for the fetch.
    38  	StartTime time.Time
    39  	// EndTime is the end time for the fetch.
    40  	EndTime time.Time
    41  	// DataOptions are the options for the fetch.
    42  	DataOptions
    43  	// QueryFetchOpts are the query storage fetch options.
    44  	QueryFetchOpts *querystorage.FetchOptions
    45  }
    46  
    47  // DataOptions provide data context.
    48  type DataOptions struct {
    49  	// Timeout determines a custom timeout for the context. If set to 0, uses
    50  	// the default timeout.
    51  	Timeout time.Duration
    52  }
    53  
    54  // Storage provides an interface for retrieving timeseries values or names
    55  // based upon a query or path.
    56  type Storage interface {
    57  	// FetchByQuery fetches timeseries data based on a query.
    58  	FetchByQuery(
    59  		ctx context.Context,
    60  		query string,
    61  		opts FetchOptions,
    62  	) (*FetchResult, error)
    63  
    64  	// CompleteTags fetches tag data based on a request.
    65  	CompleteTags(
    66  		ctx stdcontext.Context,
    67  		query *querystorage.CompleteTagsQuery,
    68  		opts *querystorage.FetchOptions,
    69  	) (*consolidators.CompleteTagsResult, error)
    70  }
    71  
    72  // FetchResult provides a fetch result and meta information.
    73  type FetchResult struct {
    74  	// SeriesList is the aggregated list of results across all underlying storage
    75  	// calls.
    76  	SeriesList []*ts.Series
    77  	// Metadata contains any additional metadata indicating information about
    78  	// series execution.
    79  	Metadata block.ResultMetadata
    80  }
    81  
    82  // Close will return the fetch result to the pool.
    83  func (fr *FetchResult) Close() error {
    84  	fr.SeriesList = nil
    85  	fr.Metadata = block.NewResultMetadata()
    86  	fetchResultPool.Put(fr)
    87  	return nil
    88  }
    89  
    90  // Reset will wipe out existing fetch result data.
    91  func (fr *FetchResult) Reset() {
    92  	fr.SeriesList = nil
    93  	fr.Metadata = block.NewResultMetadata()
    94  }
    95  
    96  var (
    97  	fetchResultPool = &sync.Pool{
    98  		New: func() interface{} {
    99  			return &FetchResult{
   100  				Metadata: block.NewResultMetadata(),
   101  			}
   102  		},
   103  	}
   104  )
   105  
   106  // NewFetchResult is a convenience method for creating a FetchResult.
   107  func NewFetchResult(
   108  	ctx context.Context,
   109  	seriesList []*ts.Series,
   110  	resultMeta block.ResultMetadata,
   111  ) *FetchResult {
   112  	fetchResult := fetchResultPool.Get().(*FetchResult)
   113  	fetchResult.Reset()
   114  	fetchResult.SeriesList = seriesList
   115  	fetchResult.Metadata = resultMeta
   116  	ctx.RegisterCloser(fetchResult)
   117  	return fetchResult
   118  }