github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/process/process.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 process
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    23  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    26  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    27  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    28  	"github.com/matrixorigin/matrixone/pkg/util/trace"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    30  )
    31  
    32  // New creates a new Process.
    33  // A process stores the execution context.
    34  func New(
    35  	ctx context.Context,
    36  	m *mpool.MPool,
    37  	txnClient client.TxnClient,
    38  	txnOperator client.TxnOperator,
    39  	fileService fileservice.FileService,
    40  	getClusterDetails engine.GetClusterDetailsFunc,
    41  ) *Process {
    42  	return &Process{
    43  		mp:                m,
    44  		Ctx:               ctx,
    45  		TxnClient:         txnClient,
    46  		TxnOperator:       txnOperator,
    47  		FileService:       fileService,
    48  		GetClusterDetails: getClusterDetails,
    49  		UnixTime:          time.Now().UnixNano(),
    50  		LastInsertID:      new(uint64),
    51  	}
    52  }
    53  
    54  func NewWithAnalyze(p *Process, ctx context.Context, regNumber int, anals []*AnalyzeInfo) *Process {
    55  	proc := NewFromProc(p, ctx, regNumber)
    56  	proc.AnalInfos = make([]*AnalyzeInfo, len(anals))
    57  	copy(proc.AnalInfos, anals)
    58  	return proc
    59  }
    60  
    61  // NewFromProc create a new Process based on another process.
    62  func NewFromProc(p *Process, ctx context.Context, regNumber int) *Process {
    63  	proc := new(Process)
    64  	newctx, cancel := context.WithCancel(ctx)
    65  	proc.Id = p.Id
    66  	proc.mp = p.Mp()
    67  	proc.Lim = p.Lim
    68  	proc.TxnClient = p.TxnClient
    69  	proc.TxnOperator = p.TxnOperator
    70  	proc.AnalInfos = p.AnalInfos
    71  	proc.SessionInfo = p.SessionInfo
    72  	proc.FileService = p.FileService
    73  	proc.GetClusterDetails = p.GetClusterDetails
    74  	proc.UnixTime = p.UnixTime
    75  	proc.LastInsertID = p.LastInsertID
    76  
    77  	// reg and cancel
    78  	proc.Ctx = newctx
    79  	proc.Cancel = cancel
    80  	proc.Reg.MergeReceivers = make([]*WaitRegister, regNumber)
    81  	for i := 0; i < regNumber; i++ {
    82  		proc.Reg.MergeReceivers[i] = &WaitRegister{
    83  			Ctx: newctx,
    84  			Ch:  make(chan *batch.Batch, 1),
    85  		}
    86  	}
    87  	proc.DispatchNotifyCh = make(chan WrapCs)
    88  	proc.LoadLocalReader = p.LoadLocalReader
    89  	return proc
    90  }
    91  
    92  func (wreg *WaitRegister) MarshalBinary() ([]byte, error) {
    93  	return nil, nil
    94  }
    95  
    96  func (wreg *WaitRegister) UnmarshalBinary(_ []byte) error {
    97  	return nil
    98  }
    99  
   100  func (proc *Process) MarshalBinary() ([]byte, error) {
   101  	return nil, nil
   102  }
   103  
   104  func (proc *Process) UnmarshalBinary(_ []byte) error {
   105  	return nil
   106  }
   107  
   108  func (proc *Process) QueryId() string {
   109  	return proc.Id
   110  }
   111  
   112  func (proc *Process) SetQueryId(id string) {
   113  	proc.Id = id
   114  }
   115  
   116  // XXX MPOOL
   117  // Some times we call an expr eval function without a proc (test only?)
   118  // in that case, all expr eval code get an nil mp which is wrong.
   119  // so far the most cases come from
   120  // plan.ConstantFold -> colexec.EvalExpr, busted.
   121  // hack in a fall back mpool.  This is by design a Zero MP so that there
   122  // will not be real leaks, except we leak counters in globalStats
   123  var xxxProcMp = mpool.MustNewZeroWithTag("fallback_proc_mp")
   124  
   125  func (proc *Process) GetMPool() *mpool.MPool {
   126  	if proc == nil {
   127  		return xxxProcMp
   128  	}
   129  	return proc.mp
   130  }
   131  
   132  func (proc *Process) Mp() *mpool.MPool {
   133  	return proc.GetMPool()
   134  }
   135  
   136  func (proc *Process) OperatorOutofMemory(size int64) bool {
   137  	return proc.Mp().Cap() < size
   138  }
   139  
   140  func (proc *Process) SetInputBatch(bat *batch.Batch) {
   141  	proc.Reg.InputBatch = bat
   142  }
   143  
   144  func (proc *Process) InputBatch() *batch.Batch {
   145  	return proc.Reg.InputBatch
   146  }
   147  
   148  func (proc *Process) GetAnalyze(idx int) Analyze {
   149  	if idx >= len(proc.AnalInfos) {
   150  		return &analyze{analInfo: nil}
   151  	}
   152  	return &analyze{analInfo: proc.AnalInfos[idx], wait: 0}
   153  }
   154  
   155  func (proc *Process) AllocVector(typ types.Type, size int64) (*vector.Vector, error) {
   156  	return proc.AllocVectorOfRows(typ, size/int64(typ.TypeSize()), nil)
   157  }
   158  
   159  func (proc *Process) AllocVectorOfRows(typ types.Type, nele int64, nsp *nulls.Nulls) (*vector.Vector, error) {
   160  	vec := vector.New(typ)
   161  	vector.PreAlloc(vec, int(nele), int(nele), proc.Mp())
   162  	if nsp != nil {
   163  		nulls.Set(vec.Nsp, nsp)
   164  	}
   165  	return vec, nil
   166  }
   167  
   168  func (proc *Process) AllocScalarVector(typ types.Type) *vector.Vector {
   169  	return vector.NewConst(typ, 1)
   170  }
   171  
   172  func (proc *Process) AllocScalarNullVector(typ types.Type) *vector.Vector {
   173  	vec := vector.NewConst(typ, 1)
   174  	nulls.Add(vec.Nsp, 0)
   175  	return vec
   176  }
   177  
   178  func (proc *Process) AllocConstNullVector(typ types.Type, cnt int) *vector.Vector {
   179  	vec := vector.NewConstNull(typ, cnt)
   180  	nulls.Add(vec.Nsp, 0)
   181  	return vec
   182  }
   183  
   184  func (proc *Process) AllocBoolScalarVector(v bool) *vector.Vector {
   185  	typ := types.T_bool.ToType()
   186  	vec := proc.AllocScalarVector(typ)
   187  	bvec := make([]bool, 1)
   188  	bvec[0] = v
   189  	vec.Col = bvec
   190  	return vec
   191  }
   192  
   193  func (proc *Process) AllocInt64ScalarVector(v int64) *vector.Vector {
   194  	typ := types.T_int64.ToType()
   195  	vec := proc.AllocScalarVector(typ)
   196  	ivec := make([]int64, 1)
   197  	ivec[0] = v
   198  	vec.Col = ivec
   199  	return vec
   200  }
   201  
   202  func (proc *Process) WithSpanContext(sc trace.SpanContext) {
   203  	proc.Ctx = trace.ContextWithSpanContext(proc.Ctx, sc)
   204  }