github.com/m3db/m3@v1.5.0/src/query/models/query_context.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 models
    22  
    23  import (
    24  	"context"
    25  
    26  	"github.com/m3db/m3/src/metrics/policy"
    27  
    28  	"github.com/uber-go/tally"
    29  )
    30  
    31  // QueryContext provides all external state needed to execute and track a query.
    32  type QueryContext struct {
    33  	Ctx     context.Context
    34  	Scope   tally.Scope
    35  	Options QueryContextOptions
    36  }
    37  
    38  // QueryContextOptions contains optional configuration for the query context.
    39  type QueryContextOptions struct {
    40  	// LimitMaxTimeseries limits the number of time series returned by each
    41  	// storage node.
    42  	LimitMaxTimeseries int
    43  	// LimitMaxDocs limits the number of docs returned by each storage node.
    44  	LimitMaxDocs int
    45  	// LimitMaxReturnedSeries limits the number of series returned in total to the client.
    46  	LimitMaxReturnedSeries int
    47  	// LimitMaxReturnedDatapoints limits the number of datapoints returned in total to the client.
    48  	LimitMaxReturnedDatapoints int
    49  	// LimitMaxReturnedSeriesMetadata limits the number of series metadata returned in total to the client.
    50  	LimitMaxReturnedSeriesMetadata int
    51  	// RequireExhaustive results in an error if the query exceeds the series limit.
    52  	RequireExhaustive bool
    53  	// Instantaneous indicates an instant query.
    54  	Instantaneous bool
    55  	// RestrictFetchType restricts the query fetches.
    56  	RestrictFetchType *RestrictFetchTypeQueryContextOptions
    57  }
    58  
    59  // RestrictFetchTypeQueryContextOptions allows for specifying the
    60  // restrict options for a query.
    61  type RestrictFetchTypeQueryContextOptions struct {
    62  	MetricsType   uint
    63  	StoragePolicy policy.StoragePolicy
    64  }
    65  
    66  // NewQueryContext constructs a QueryContext from the provided native context.
    67  func NewQueryContext(
    68  	ctx context.Context,
    69  	scope tally.Scope,
    70  	options QueryContextOptions,
    71  ) *QueryContext {
    72  	return &QueryContext{
    73  		Ctx:     ctx,
    74  		Scope:   scope,
    75  		Options: options,
    76  	}
    77  }
    78  
    79  // NoopQueryContext returns a query context with no active components.
    80  func NoopQueryContext() *QueryContext {
    81  	return NewQueryContext(context.Background(), tally.NoopScope,
    82  		QueryContextOptions{})
    83  }
    84  
    85  // WithContext creates a shallow copy of this QueryContext using the new context.
    86  // Sample usage:
    87  //
    88  // ctx, cancel := context.WithTimeout(qc.Ctx, 5*time.Second)
    89  // defer cancel()
    90  // qc = qc.WithContext(ctx)
    91  func (qc *QueryContext) WithContext(ctx context.Context) *QueryContext {
    92  	if qc == nil {
    93  		return nil
    94  	}
    95  
    96  	clone := *qc
    97  	clone.Ctx = ctx
    98  	return &clone
    99  }