github.com/m3db/m3@v1.5.0/src/dbnode/client/aggregate_attempt.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 client
    22  
    23  import (
    24  	"context"
    25  
    26  	"github.com/m3db/m3/src/dbnode/storage/index"
    27  	"github.com/m3db/m3/src/x/ident"
    28  	"github.com/m3db/m3/src/x/pool"
    29  	xretry "github.com/m3db/m3/src/x/retry"
    30  )
    31  
    32  var (
    33  	aggregateAttemptArgsZeroed aggregateAttemptArgs
    34  )
    35  
    36  type aggregateAttempt struct {
    37  	session   *session
    38  	attemptFn xretry.Fn
    39  
    40  	args           aggregateAttemptArgs
    41  	resultIter     AggregatedTagsIterator
    42  	resultMetadata FetchResponseMetadata
    43  }
    44  
    45  type aggregateAttemptArgs struct {
    46  	ctx   context.Context
    47  	ns    ident.ID
    48  	query index.Query
    49  	opts  index.AggregationOptions
    50  }
    51  
    52  func (f *aggregateAttempt) reset() {
    53  	f.args = aggregateAttemptArgsZeroed
    54  	f.resultIter = nil
    55  	f.resultMetadata = FetchResponseMetadata{}
    56  }
    57  
    58  func (f *aggregateAttempt) performAttempt() error {
    59  	var err error
    60  	f.resultIter, f.resultMetadata, err = f.session.aggregateAttempt(f.args.ctx,
    61  		f.args.ns, f.args.query, f.args.opts)
    62  	return err
    63  }
    64  
    65  type aggregateAttemptPool interface {
    66  	Init()
    67  	Get() *aggregateAttempt
    68  	Put(*aggregateAttempt)
    69  }
    70  
    71  type aggregateAttemptPoolImpl struct {
    72  	pool    pool.ObjectPool
    73  	session *session
    74  }
    75  
    76  func newAggregateAttemptPool(
    77  	session *session,
    78  	opts pool.ObjectPoolOptions,
    79  ) aggregateAttemptPool {
    80  	p := pool.NewObjectPool(opts)
    81  	return &aggregateAttemptPoolImpl{pool: p, session: session}
    82  }
    83  
    84  func (p *aggregateAttemptPoolImpl) Init() {
    85  	p.pool.Init(func() interface{} {
    86  		f := &aggregateAttempt{session: p.session}
    87  		// NB(prateek): Bind fn once to avoid creating receiver
    88  		// and function method pointer over and over again
    89  		f.attemptFn = f.performAttempt
    90  		f.reset()
    91  		return f
    92  	})
    93  }
    94  
    95  func (p *aggregateAttemptPoolImpl) Get() *aggregateAttempt {
    96  	return p.pool.Get().(*aggregateAttempt)
    97  }
    98  
    99  func (p *aggregateAttemptPoolImpl) Put(f *aggregateAttempt) {
   100  	f.reset()
   101  	p.pool.Put(f)
   102  }