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

     1  // Copyright 2021 - 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  	"strconv"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/clusterservice"
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/query"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    28  )
    29  
    30  func getTargets(param string) []string {
    31  	cns := strings.Split(param, ",")
    32  	if len(cns) == 1 && strings.ToLower(cns[0]) == "all" {
    33  		cns = make([]string, 0)
    34  		clusterservice.GetMOCluster().GetCNService(clusterservice.Selector{}, func(cn metadata.CNService) bool {
    35  			cns = append(cns, cn.ServiceID)
    36  			return true
    37  		})
    38  	}
    39  	return cns
    40  }
    41  
    42  // handleUnsubscribeTable unsubscribes a table from logtail client.
    43  func handleUnsubscribeTable(proc *process.Process,
    44  	service serviceType,
    45  	parameter string,
    46  	_ requestSender) (Result, error) {
    47  	if service != cn {
    48  		return Result{}, moerr.NewWrongServiceNoCtx("CN", string(service))
    49  	}
    50  	args := strings.Split(parameter, ":")
    51  	if len(args) != 3 {
    52  		return Result{}, moerr.NewInternalErrorNoCtx("parameter invalid")
    53  	}
    54  	dbID, err := strconv.ParseUint(args[1], 10, 64)
    55  	if err != nil {
    56  		return Result{}, moerr.NewInternalErrorNoCtx("wrong database ID: %s", args[1])
    57  	}
    58  	tbID, err := strconv.ParseUint(args[2], 10, 64)
    59  	if err != nil {
    60  		return Result{}, moerr.NewInternalErrorNoCtx("wrong table ID: %s", args[2])
    61  	}
    62  	targets := getTargets(args[0])
    63  	for _, c := range targets {
    64  		if err := doUnsubscribeTable(proc, c, dbID, tbID); err != nil {
    65  			return Result{}, err
    66  		}
    67  	}
    68  	return Result{
    69  		Method: UnsubscribeTable,
    70  		Data:   "OK",
    71  	}, nil
    72  }
    73  
    74  func doUnsubscribeTable(proc *process.Process, uuid string, dbID, tbID uint64) error {
    75  	var err error
    76  	clusterservice.GetMOCluster().GetCNService(clusterservice.NewServiceIDSelector(uuid),
    77  		func(cn metadata.CNService) bool {
    78  			request := proc.QueryClient.NewRequest(query.CmdMethod_UnsubscribeTable)
    79  			request.UnsubscribeTable = &query.UnsubscribeTableRequest{
    80  				DatabaseID: dbID,
    81  				TableID:    tbID,
    82  			}
    83  			ctx, cancel := context.WithTimeout(proc.Ctx, time.Second*3)
    84  			defer cancel()
    85  			resp, err := proc.QueryClient.SendMessage(ctx, cn.QueryAddress, request)
    86  			if err != nil {
    87  				return false
    88  			}
    89  			if resp != nil {
    90  				proc.QueryClient.Release(resp)
    91  			}
    92  			return true
    93  		})
    94  	return err
    95  }