github.com/m3db/m3@v1.5.0/src/query/executor/result.go (about)

     1  // Copyright (c) 2018 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 executor
    22  
    23  import (
    24  	"sync"
    25  
    26  	"github.com/m3db/m3/src/query/block"
    27  	"github.com/m3db/m3/src/query/executor/transform"
    28  	"github.com/m3db/m3/src/query/models"
    29  	"github.com/m3db/m3/src/query/parser"
    30  	"github.com/pkg/errors"
    31  )
    32  
    33  type sink interface {
    34  	transform.OpNode
    35  	closeWithError(err error)
    36  	getValue() (block.Block, error)
    37  }
    38  
    39  // resultNode collects final blocks.
    40  type resultNode struct {
    41  	sync.RWMutex
    42  	wg sync.WaitGroup
    43  
    44  	err       error
    45  	completed bool
    46  	block     block.Block
    47  }
    48  
    49  func newResultNode() sink {
    50  	node := &resultNode{}
    51  	node.wg.Add(1)
    52  	return node
    53  }
    54  
    55  func (r *resultNode) closeWithLock() {
    56  	r.wg.Done()
    57  	r.completed = true
    58  }
    59  
    60  // Process sets the incoming block and releases the wait group.
    61  func (r *resultNode) Process(_ *models.QueryContext,
    62  	_ parser.NodeID, block block.Block) error {
    63  	r.Lock()
    64  	defer r.Unlock()
    65  
    66  	if r.err != nil {
    67  		return r.err
    68  	}
    69  
    70  	if r.block != nil {
    71  		r.err = errors.New("resultNode block already set")
    72  		return r.err
    73  	}
    74  
    75  	r.block = block
    76  	r.closeWithLock()
    77  	return nil
    78  }
    79  
    80  func (r *resultNode) closeWithError(err error) {
    81  	r.Lock()
    82  	defer r.Unlock()
    83  	if r.completed {
    84  		return
    85  	}
    86  
    87  	r.err = err
    88  	r.closeWithLock()
    89  }
    90  
    91  func (r *resultNode) getValue() (block.Block, error) {
    92  	r.wg.Wait()
    93  	r.RLock()
    94  	bl, err := r.block, r.err
    95  	r.RUnlock()
    96  	return bl, err
    97  }