github.com/m3db/m3@v1.5.0/src/query/executor/transform/exec.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 transform
    22  
    23  import (
    24  	"github.com/m3db/m3/src/query/block"
    25  	"github.com/m3db/m3/src/query/models"
    26  	"github.com/m3db/m3/src/query/parser"
    27  	"github.com/m3db/m3/src/x/opentracing"
    28  )
    29  
    30  // simpleOpNode defines the contract for OpNode instances which
    31  type simpleOpNode interface {
    32  	Params() parser.Params
    33  	ProcessBlock(queryCtx *models.QueryContext, ID parser.NodeID, b block.Block) (block.Block, error)
    34  }
    35  
    36  // ProcessSimpleBlock is a utility for OpNode instances which on receiving a block, process and propagate it immediately
    37  // (as opposed to nodes which e.g. depend on multiple blocks).
    38  // It adds instrumentation to the processing, and handles propagating the block downstream.
    39  // OpNode's should call this as their implementation of the Process method:
    40  //
    41  // func (n MyNode) Process(queryCtx *models.QueryContext, ID parser.NodeID, b block.Block) error {
    42  //     return transform.ProcessSimpleBlock(n, n.controller, queryCtx, ID, b)
    43  // }
    44  func ProcessSimpleBlock(
    45  	node simpleOpNode,
    46  	controller *Controller,
    47  	queryCtx *models.QueryContext,
    48  	ID parser.NodeID,
    49  	b block.Block,
    50  ) error {
    51  	sp, ctx := opentracing.StartSpanFromContext(queryCtx.Ctx, node.Params().OpType())
    52  	nextBlock, err := node.ProcessBlock(queryCtx.WithContext(ctx), ID, b)
    53  	sp.Finish()
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	// NB: The flow here is a little weird; this kicks off the next block's
    59  	// processing step after retrieving it, then attempts to close it. There is a
    60  	// trick here where some blocks (specifically lazy wrappers) that should not
    61  	// be closed, as they would free underlying data. The general story in block
    62  	// lifecycle should be revisited to remove quirks arising from these edge
    63  	// cases (something where blocks are responsible for calling their own
    64  	// downstreams would seem more intuitive and allow finer grained lifecycle
    65  	// control).
    66  	err = controller.Process(queryCtx, nextBlock)
    67  	if nextBlock.Info().Type() != block.BlockLazy {
    68  		nextBlock.Close()
    69  	}
    70  
    71  	return err
    72  }