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

     1  // Copyright 2024 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  	"strings"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/txn/trace"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  // select mo_ctl('cn', 'txn-trace', 'enable|disable|clear|add|decode-complex')
    26  func handleTxnTrace(
    27  	proc *process.Process,
    28  	service serviceType,
    29  	parameter string,
    30  	sender requestSender) (Result, error) {
    31  	params := strings.Split(parameter, " ")
    32  	if len(params) == 0 {
    33  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
    34  	}
    35  
    36  	switch strings.ToLower(params[0]) {
    37  	case "enable":
    38  		if len(params) < 2 {
    39  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
    40  		}
    41  
    42  		err := trace.GetService().Enable(params[1])
    43  		if err != nil {
    44  			return Result{}, err
    45  		}
    46  		return Result{Data: "OK"}, nil
    47  	case "disable":
    48  		if len(params) < 2 {
    49  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
    50  		}
    51  
    52  		err := trace.GetService().Disable(params[1])
    53  		if err != nil {
    54  			return Result{}, err
    55  		}
    56  		return Result{Data: "OK"}, nil
    57  	case "clear":
    58  		if len(params) < 2 || (params[1] != trace.FeatureTraceTxn && params[1] != trace.FeatureTraceData) {
    59  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
    60  		}
    61  
    62  		var err error
    63  		if params[1] == trace.FeatureTraceTxn {
    64  			err = trace.GetService().ClearTxnFilters()
    65  		} else if params[1] == trace.FeatureTraceData {
    66  			err = trace.GetService().ClearTableFilters()
    67  		} else if params[1] == trace.FeatureTraceStatement {
    68  			err = trace.GetService().ClearStatementFilters()
    69  		}
    70  
    71  		if err != nil {
    72  			return Result{}, err
    73  		}
    74  		return Result{Data: "OK"}, nil
    75  	case "refresh":
    76  		if len(params) < 2 ||
    77  			(params[1] != trace.FeatureTraceTxn &&
    78  				params[1] != trace.FeatureTraceData &&
    79  				params[1] != trace.FeatureTraceStatement) {
    80  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
    81  		}
    82  
    83  		var err error
    84  		if params[1] == trace.FeatureTraceTxn {
    85  			err = trace.GetService().RefreshTxnFilters()
    86  		} else if params[1] == trace.FeatureTraceData {
    87  			err = trace.GetService().RefreshTableFilters()
    88  		} else if params[1] == trace.FeatureTraceStatement {
    89  			err = trace.GetService().RefreshStatementFilters()
    90  		}
    91  
    92  		if err != nil {
    93  			return Result{}, err
    94  		}
    95  		return Result{Data: "OK"}, nil
    96  	case "add-table":
    97  		// add table [column1, column2, ...]
    98  		if len(params) < 2 {
    99  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
   100  		}
   101  		var columns []string
   102  		table := params[1]
   103  		if len(params) > 2 {
   104  			columns = params[2:]
   105  		}
   106  
   107  		err := trace.GetService().AddTableFilter(table, columns)
   108  		if err != nil {
   109  			return Result{}, err
   110  		}
   111  		return Result{Data: "OK"}, nil
   112  	case "add-txn":
   113  		// add-txn method value
   114  		if len(params) < 3 {
   115  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
   116  		}
   117  
   118  		err := trace.GetService().AddTxnFilter(params[1], params[2])
   119  		if err != nil {
   120  			return Result{}, err
   121  		}
   122  		return Result{Data: "OK"}, nil
   123  	case "add-statement":
   124  		// add-statement method value
   125  		if len(params) < 3 {
   126  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
   127  		}
   128  
   129  		err := trace.GetService().AddStatementFilter(params[1], params[2])
   130  		if err != nil {
   131  			return Result{}, err
   132  		}
   133  		return Result{Data: "OK"}, nil
   134  	case "decode-complex":
   135  		// decode complex pk
   136  		if len(params) != 2 {
   137  			return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
   138  		}
   139  		value, err := trace.GetService().DecodeHexComplexPK(params[1])
   140  		if err != nil {
   141  			return Result{}, err
   142  		}
   143  		return Result{Data: value}, nil
   144  	default:
   145  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s", parameter)
   146  	}
   147  }