github.com/m3db/m3@v1.5.0/src/dbnode/client/fetch_attempt.go (about)

     1  // Copyright (c) 2017 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  	"github.com/m3db/m3/src/dbnode/encoding"
    25  	xerrors "github.com/m3db/m3/src/x/errors"
    26  	"github.com/m3db/m3/src/x/ident"
    27  	"github.com/m3db/m3/src/x/pool"
    28  	xretry "github.com/m3db/m3/src/x/retry"
    29  	xtime "github.com/m3db/m3/src/x/time"
    30  )
    31  
    32  var fetchAttemptArgsZeroed fetchAttemptArgs
    33  
    34  type fetchAttempt struct {
    35  	args fetchAttemptArgs
    36  
    37  	result encoding.SeriesIterators
    38  
    39  	session *session
    40  
    41  	attemptFn xretry.Fn
    42  }
    43  
    44  type fetchAttemptArgs struct {
    45  	namespace ident.ID
    46  	ids       ident.Iterator
    47  	start     xtime.UnixNano
    48  	end       xtime.UnixNano
    49  }
    50  
    51  func (f *fetchAttempt) reset() {
    52  	f.args = fetchAttemptArgsZeroed
    53  	f.result = nil
    54  }
    55  
    56  func (f *fetchAttempt) perform() error {
    57  	result, err := f.session.fetchIDsAttempt(f.args.namespace,
    58  		f.args.ids, f.args.start, f.args.end)
    59  	f.result = result
    60  
    61  	if IsBadRequestError(err) {
    62  		// Do not retry bad request errors
    63  		err = xerrors.NewNonRetryableError(err)
    64  	}
    65  
    66  	return err
    67  }
    68  
    69  type fetchAttemptPool struct {
    70  	pool    pool.ObjectPool
    71  	session *session
    72  }
    73  
    74  func newFetchAttemptPool(
    75  	session *session,
    76  	opts pool.ObjectPoolOptions,
    77  ) *fetchAttemptPool {
    78  	p := pool.NewObjectPool(opts)
    79  	return &fetchAttemptPool{pool: p, session: session}
    80  }
    81  
    82  func (p *fetchAttemptPool) Init() {
    83  	p.pool.Init(func() interface{} {
    84  		w := &fetchAttempt{session: p.session}
    85  		// NB(r): Bind attemptFn once to avoid creating receiver
    86  		// and function method pointer over and over again
    87  		w.attemptFn = w.perform
    88  		w.reset()
    89  		return w
    90  	})
    91  }
    92  
    93  func (p *fetchAttemptPool) Get() *fetchAttempt {
    94  	return p.pool.Get().(*fetchAttempt)
    95  }
    96  
    97  func (p *fetchAttemptPool) Put(f *fetchAttempt) {
    98  	f.reset()
    99  	p.pool.Put(f)
   100  }