github.com/m3db/m3@v1.5.0/src/dbnode/client/write_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  	xerrors "github.com/m3db/m3/src/x/errors"
    25  	"github.com/m3db/m3/src/x/ident"
    26  	"github.com/m3db/m3/src/x/pool"
    27  	xretry "github.com/m3db/m3/src/x/retry"
    28  	xtime "github.com/m3db/m3/src/x/time"
    29  )
    30  
    31  type writeAttemptType byte
    32  
    33  // nolint
    34  const (
    35  	unknownWriteAttemptType writeAttemptType = iota
    36  	untaggedWriteAttemptType
    37  	taggedWriteAttemptType
    38  )
    39  
    40  var writeAttemptArgsZeroed writeAttemptArgs
    41  
    42  type writeAttempt struct {
    43  	args writeAttemptArgs
    44  
    45  	session *session
    46  
    47  	attemptFn xretry.Fn
    48  }
    49  
    50  type writeAttemptArgs struct {
    51  	namespace   ident.ID
    52  	id          ident.ID
    53  	tags        ident.TagIterator
    54  	t           xtime.UnixNano
    55  	value       float64
    56  	annotation  []byte
    57  	unit        xtime.Unit
    58  	attemptType writeAttemptType
    59  }
    60  
    61  func (w *writeAttempt) reset() {
    62  	w.args = writeAttemptArgsZeroed
    63  }
    64  
    65  func (w *writeAttempt) perform() error {
    66  	err := w.session.writeAttempt(w.args.attemptType,
    67  		w.args.namespace, w.args.id, w.args.tags, w.args.t,
    68  		w.args.value, w.args.unit, w.args.annotation)
    69  
    70  	if IsBadRequestError(err) {
    71  		// Do not retry bad request errors
    72  		err = xerrors.NewNonRetryableError(err)
    73  	}
    74  
    75  	return err
    76  }
    77  
    78  type writeAttemptPool struct {
    79  	pool    pool.ObjectPool
    80  	session *session
    81  }
    82  
    83  func newWriteAttemptPool(
    84  	session *session,
    85  	opts pool.ObjectPoolOptions,
    86  ) *writeAttemptPool {
    87  	p := pool.NewObjectPool(opts)
    88  	return &writeAttemptPool{pool: p, session: session}
    89  }
    90  
    91  func (p *writeAttemptPool) Init() {
    92  	p.pool.Init(func() interface{} {
    93  		w := &writeAttempt{session: p.session}
    94  		// NB(r): Bind attemptFn once to avoid creating receiver
    95  		// and function method pointer over and over again
    96  		w.attemptFn = w.perform
    97  		w.reset()
    98  		return w
    99  	})
   100  }
   101  
   102  func (p *writeAttemptPool) Get() *writeAttempt {
   103  	return p.pool.Get().(*writeAttempt)
   104  }
   105  
   106  func (p *writeAttemptPool) Put(w *writeAttempt) {
   107  	w.reset()
   108  	p.pool.Put(w)
   109  }