github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/dispatch/dispatch.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dispatch
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/sql/colexec"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  )
    25  
    26  func String(arg any, buf *bytes.Buffer) {
    27  	buf.WriteString("dispatch")
    28  }
    29  
    30  func Prepare(proc *process.Process, arg any) error {
    31  	ap := arg.(*Argument)
    32  	ap.ctr = new(container)
    33  
    34  	switch ap.FuncId {
    35  	case SendToAllFunc:
    36  		if len(ap.RemoteRegs) == 0 {
    37  			return moerr.NewInternalError(proc.Ctx, "SendToAllFunc should include RemoteRegs")
    38  		}
    39  		ap.prepared = false
    40  		ap.ctr.remoteReceivers = make([]*WrapperClientSession, 0, len(ap.RemoteRegs))
    41  		ap.ctr.sendFunc = sendToAllFunc
    42  		for _, rr := range ap.RemoteRegs {
    43  			colexec.Srv.PutNotifyChIntoUuidMap(rr.Uuid, proc.DispatchNotifyCh)
    44  		}
    45  
    46  	case SendToAllLocalFunc:
    47  		if len(ap.RemoteRegs) != 0 {
    48  			return moerr.NewInternalError(proc.Ctx, "SendToAllLocalFunc should not send to remote")
    49  		}
    50  		ap.prepared = true
    51  		ap.ctr.remoteReceivers = nil
    52  		ap.ctr.sendFunc = sendToAllLocalFunc
    53  	case SendToAnyLocalFunc:
    54  		if len(ap.RemoteRegs) != 0 {
    55  			return moerr.NewInternalError(proc.Ctx, "SendToAnyLocalFunc should not send to remote")
    56  		}
    57  		ap.prepared = true
    58  		ap.ctr.remoteReceivers = nil
    59  		ap.ctr.sendFunc = sendToAnyLocalFunc
    60  	default:
    61  		return moerr.NewInternalError(proc.Ctx, "wrong sendFunc id for dispatch")
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func Call(idx int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) {
    68  	ap := arg.(*Argument)
    69  
    70  	// waiting all remote receive prepared
    71  	// put it in Call() for better parallel
    72  
    73  	bat := proc.InputBatch()
    74  	if bat == nil {
    75  		return true, nil
    76  	}
    77  
    78  	if bat.Length() == 0 {
    79  		return false, nil
    80  	}
    81  
    82  	for i, vec := range bat.Vecs {
    83  		if vec.IsOriginal() {
    84  			cloneVec, err := vector.Dup(vec, proc.Mp())
    85  			if err != nil {
    86  				bat.Clean(proc.Mp())
    87  				return false, err
    88  			}
    89  			bat.Vecs[i] = cloneVec
    90  		}
    91  	}
    92  
    93  	if err := ap.ctr.sendFunc(bat, ap, proc); err != nil {
    94  		return false, err
    95  	}
    96  	if len(ap.LocalRegs) == 0 {
    97  		return true, nil
    98  	}
    99  	proc.SetInputBatch(nil)
   100  	return false, nil
   101  }