github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/ctl/cmd_lock_service.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  	"context"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/fagongzi/util/format"
    24  	"github.com/matrixorigin/matrixone/pkg/clusterservice"
    25  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    27  	querypb "github.com/matrixorigin/matrixone/pkg/pb/query"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    29  )
    30  
    31  func handleRemoveRemoteLockTable(
    32  	proc *process.Process,
    33  	service serviceType,
    34  	parameter string,
    35  	sender requestSender) (Result, error) {
    36  	infos := strings.Split(parameter, "-")
    37  	if len(infos) != 3 {
    38  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s, use account_id + '-' + table_id + '-' + bind_version", parameter)
    39  	}
    40  
    41  	accountID, err := format.ParseStringUint32(infos[0])
    42  	if err != nil {
    43  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s, use account_id + '-' + table_id + '-' + bind_version", parameter)
    44  	}
    45  
    46  	tableID, err := format.ParseStringUint64(infos[1])
    47  	if err != nil {
    48  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s, use account_id + '-' + table_id + '-' + bind_version", parameter)
    49  	}
    50  
    51  	version, err := format.ParseStringUint64(infos[2])
    52  	if err != nil {
    53  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s, use account_id + '-' + table_id + '-' + bind_version", parameter)
    54  	}
    55  
    56  	qt := proc.QueryClient
    57  	mc := clusterservice.GetMOCluster()
    58  	var addrs []string
    59  	mc.GetCNServiceWithoutWorkingState(
    60  		clusterservice.NewSelector(),
    61  		func(c metadata.CNService) bool {
    62  			addrs = append(addrs, c.QueryAddress)
    63  			return true
    64  		})
    65  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    66  	defer cancel()
    67  
    68  	total := 0
    69  	for _, addr := range addrs {
    70  		req := qt.NewRequest(querypb.CmdMethod_RemoveRemoteLockTable)
    71  		req.RemoveRemoteLockTable = &querypb.RemoveRemoteLockTableRequest{
    72  			GroupID: accountID,
    73  			TableID: tableID,
    74  			Version: version,
    75  		}
    76  
    77  		resp, err := qt.SendMessage(ctx, addr, req)
    78  		if err != nil {
    79  			return Result{}, err
    80  		}
    81  		total += int(resp.RemoveRemoteLockTable.Count)
    82  		qt.Release(resp)
    83  	}
    84  
    85  	return Result{
    86  		Method: RemoveRemoteLockTable,
    87  		Data:   fmt.Sprintf("remote %d in all cn", total),
    88  	}, nil
    89  }
    90  
    91  func handleGetLatestBind(
    92  	proc *process.Process,
    93  	service serviceType,
    94  	parameter string,
    95  	sender requestSender) (Result, error) {
    96  	infos := strings.Split(parameter, "-")
    97  	if len(infos) != 2 {
    98  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s, use account_id + '-' + table_id", parameter)
    99  	}
   100  
   101  	accountID, err := format.ParseStringUint32(infos[0])
   102  	if err != nil {
   103  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s, use account_id + '-' + table_id", parameter)
   104  	}
   105  
   106  	tableID, err := format.ParseStringUint64(infos[1])
   107  	if err != nil {
   108  		return Result{}, moerr.NewInvalidInputNoCtx("invalid parameter %s, use account_id + '-' + table_id", parameter)
   109  	}
   110  
   111  	qt := proc.QueryClient
   112  	mc := clusterservice.GetMOCluster()
   113  	var addr string
   114  	mc.GetTNService(
   115  		clusterservice.NewSelector(),
   116  		func(t metadata.TNService) bool {
   117  			addr = t.QueryAddress
   118  			return true
   119  		})
   120  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
   121  	defer cancel()
   122  
   123  	req := qt.NewRequest(querypb.CmdMethod_GetLatestBind)
   124  	req.GetLatestBind = &querypb.GetLatestBindRequest{
   125  		GroupID: accountID,
   126  		TableID: tableID,
   127  	}
   128  
   129  	resp, err := qt.SendMessage(ctx, addr, req)
   130  	if err != nil {
   131  		return Result{}, err
   132  	}
   133  	defer qt.Release(resp)
   134  
   135  	return Result{
   136  		Method: GetLatestBind,
   137  		Data:   resp.GetLatestBind.Bind,
   138  	}, nil
   139  }