github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/ctl/ctl.go (about)

     1  // Copyright 2022 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 ctl
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    25  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function/functionUtil"
    26  	"github.com/matrixorigin/matrixone/pkg/util/json"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    29  )
    30  
    31  var MoCtlTNCmdSender = func(ctx context.Context, proc *process.Process, requests []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
    32  	txnOp := proc.TxnOperator
    33  	if txnOp == nil {
    34  		return nil, moerr.NewInternalError(ctx, "ctl: txn operator is nil")
    35  	}
    36  
    37  	debugRequests := make([]txn.TxnRequest, 0, len(requests))
    38  	for _, req := range requests {
    39  		tq := txn.NewTxnRequest(&req)
    40  		tq.Method = txn.TxnMethod_DEBUG
    41  		debugRequests = append(debugRequests, tq)
    42  	}
    43  	result, err := txnOp.Debug(ctx, debugRequests)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	defer result.Release()
    48  
    49  	responses := make([]txn.CNOpResponse, 0, len(requests))
    50  	for _, resp := range result.Responses {
    51  		responses = append(responses, *resp.CNOpResponse)
    52  	}
    53  	return responses, nil
    54  }
    55  
    56  // mo_ctl functions are significantly different from oridinary functions and
    57  // deserve its own package.
    58  func MoCtl(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
    59  	rs := vector.MustFunctionResult[types.Varlena](result)
    60  	args0 := vector.GenerateFunctionStrParameter(ivecs[0])
    61  	args1 := vector.GenerateFunctionStrParameter(ivecs[1])
    62  	args2 := vector.GenerateFunctionStrParameter(ivecs[2])
    63  
    64  	if length != 1 {
    65  		return moerr.NewInvalidInput(proc.Ctx, "mo_ctl can only be called with const args")
    66  	}
    67  
    68  	arg0, _ := args0.GetStrValue(0)
    69  	arg1, _ := args1.GetStrValue(0)
    70  	arg2, _ := args2.GetStrValue(0)
    71  
    72  	service := serviceType(strings.ToUpper(functionUtil.QuickBytesToStr(arg0)))
    73  	command := strings.ToUpper(functionUtil.QuickBytesToStr(arg1))
    74  	parameter := functionUtil.QuickBytesToStr(arg2)
    75  
    76  	if _, ok := supportedServiceTypes[service]; !ok {
    77  		return moerr.NewNotSupported(proc.Ctx, "service type %s not supported", service)
    78  	}
    79  
    80  	f, ok := supportedCmds[command]
    81  	if !ok {
    82  		return moerr.NewNotSupported(proc.Ctx, "command %s not supported", command)
    83  	}
    84  
    85  	res, err := f(proc,
    86  		service,
    87  		parameter,
    88  		// We use a transaction client to send debug requests with the following in mind.
    89  		// 1. reuse the RPC mechanism of cn and dn
    90  		// 2. may support debug support for transactions in the future, such as testing the
    91  		//    correctness of the transaction by forcing the timestamp of the transaction to
    92  		//    be modified, etc.
    93  		// TODO: add more ut tests for this.
    94  		MoCtlTNCmdSender)
    95  
    96  	if err != nil {
    97  		return err
    98  	}
    99  	if command == InspectMethod || command == MergeObjectsMethod {
   100  		obj := res.Data.([]any)[0].(*db.InspectResp)
   101  		err = rs.AppendBytes([]byte(obj.ConsoleString()), false)
   102  		return err
   103  	}
   104  	err = rs.AppendBytes(json.Pretty(res), false)
   105  	return err
   106  }