github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/dbutils/runtime.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 dbutils
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/logutil"
    23  	"github.com/matrixorigin/matrixone/pkg/objectio"
    24  	"github.com/matrixorigin/matrixone/pkg/util/metric/stats"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/model"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks"
    29  )
    30  
    31  type RuntimeOption func(*Runtime)
    32  
    33  func WithRuntimeSmallPool(vp *containers.VectorPool) RuntimeOption {
    34  	return func(r *Runtime) {
    35  		r.VectorPool.Small = vp
    36  	}
    37  }
    38  
    39  func WithRuntimeTransientPool(vp *containers.VectorPool) RuntimeOption {
    40  	return func(r *Runtime) {
    41  		r.VectorPool.Transient = vp
    42  	}
    43  }
    44  
    45  func WithRuntimeObjectFS(fs *objectio.ObjectFS) RuntimeOption {
    46  	return func(r *Runtime) {
    47  		r.Fs = fs
    48  	}
    49  }
    50  
    51  func WithRuntimeTransferTable(tt *model.HashPageTable) RuntimeOption {
    52  	return func(r *Runtime) {
    53  		r.TransferTable = tt
    54  	}
    55  }
    56  
    57  func WithRuntimeScheduler(s tasks.TaskScheduler) RuntimeOption {
    58  	return func(r *Runtime) {
    59  		r.Scheduler = s
    60  	}
    61  }
    62  
    63  func WithRuntimeOptions(opts *options.Options) RuntimeOption {
    64  	return func(r *Runtime) {
    65  		r.Options = opts
    66  	}
    67  }
    68  
    69  type Runtime struct {
    70  	Now        func() types.TS
    71  	VectorPool struct {
    72  		Small     *containers.VectorPool
    73  		Transient *containers.VectorPool
    74  	}
    75  
    76  	Fs *objectio.ObjectFS
    77  
    78  	TransferTable   *model.HashPageTable
    79  	TransferDelsMap *model.TransDelsForBlks
    80  	Scheduler       tasks.TaskScheduler
    81  
    82  	Options *options.Options
    83  
    84  	Logtail struct {
    85  		CompactStats stats.Counter
    86  	}
    87  }
    88  
    89  func NewRuntime(opts ...RuntimeOption) *Runtime {
    90  	r := new(Runtime)
    91  	r.TransferDelsMap = model.NewTransDelsForBlks()
    92  	for _, opt := range opts {
    93  		opt(r)
    94  	}
    95  	r.fillDefaults()
    96  	return r
    97  }
    98  
    99  func (r *Runtime) fillDefaults() {
   100  	if r.VectorPool.Small == nil {
   101  		r.VectorPool.Small = MakeDefaultSmallPool("small-vector-pool")
   102  	}
   103  	if r.VectorPool.Transient == nil {
   104  		r.VectorPool.Transient = MakeDefaultTransientPool("trasient-vector-pool")
   105  	}
   106  }
   107  
   108  func (r *Runtime) PrintVectorPoolUsage() {
   109  	var w bytes.Buffer
   110  	w.WriteString(r.VectorPool.Transient.String())
   111  	w.WriteByte('\n')
   112  	w.WriteString(r.VectorPool.Small.String())
   113  	logutil.Info(w.String())
   114  }
   115  
   116  func (r *Runtime) ExportLogtailStats() string {
   117  	return fmt.Sprintf(
   118  		"LogtailStats: Compact[%d|%d]",
   119  		r.Logtail.CompactStats.SwapW(0),
   120  		r.Logtail.CompactStats.Load(),
   121  	)
   122  }