github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/ctl/master/purge_relay.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package master
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"os"
    20  	"strings"
    21  
    22  	"github.com/pingcap/errors"
    23  	"github.com/pingcap/tiflow/dm/ctl/common"
    24  	"github.com/pingcap/tiflow/dm/pb"
    25  	"github.com/pingcap/tiflow/dm/pkg/utils"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  // NewPurgeRelayCmd creates a PurgeRelay command
    30  // three purge methods supported by dmctl
    31  // 1. purge inactive relay log files
    32  // 2. purge before time, like `PURGE BINARY LOGS BEFORE` in MySQL
    33  // 3. purge before filename, like `PURGE BINARY LOGS TO`.
    34  func NewPurgeRelayCmd() *cobra.Command {
    35  	cmd := &cobra.Command{
    36  		// Use:   "purge-relay <-w worker> [--inactive] [--time] [--filename] [--sub-dir]",
    37  		// Short: "purge dm-worker's relay log files, choose 1 of 2 methods",
    38  		Use:   "purge-relay <-s source> <-f filename> [--sub-dir directory]",
    39  		Short: "Purges relay log files of the DM-worker according to the specified filename",
    40  		RunE:  purgeRelayFunc,
    41  	}
    42  	// cmd.Flags().BoolP("inactive", "i", false, "whether try to purge all inactive relay log files")
    43  	// cmd.Flags().StringP("time", "t", "", fmt.Sprintf("whether try to purge relay log files before this time, the format is \"%s\"(_ between date and time)", timeFormat))
    44  	cmd.Flags().StringP("filename", "f", "", "name of the terminal file before which to purge relay log files. Sample format: \"mysql-bin.000006\"")
    45  	cmd.Flags().StringP("sub-dir", "", "", "specify relay sub directory for --filename. If not specified, the latest one will be used. Sample format: \"2ae76434-f79f-11e8-bde2-0242ac130008.000001\"")
    46  
    47  	return cmd
    48  }
    49  
    50  // purgeRelayFunc does purge relay log files.
    51  func purgeRelayFunc(cmd *cobra.Command, _ []string) error {
    52  	if len(cmd.Flags().Args()) > 0 {
    53  		cmd.SetOut(os.Stdout)
    54  		common.PrintCmdUsage(cmd)
    55  		return errors.New("please check output to see error")
    56  	}
    57  
    58  	sources, err := common.GetSourceArgs(cmd)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	if len(sources) == 0 {
    63  		return errors.New("must specify at least one source (`-s` / `--source`)")
    64  	}
    65  
    66  	filename, err := cmd.Flags().GetString("filename")
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if len(filename) == 0 {
    72  		return errors.New("must specify the name of the terminal file before which to purge relay log files. (`-f` / `--filename`)")
    73  	}
    74  
    75  	subDir, err := cmd.Flags().GetString("sub-dir")
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	if len(filename) > 0 {
    81  		// count++
    82  		filename = strings.Trim(filename, "\"")
    83  	}
    84  
    85  	if len(filename) > 0 && len(sources) > 1 {
    86  		return errors.New("for --filename, can only specify one source per time")
    87  	}
    88  	if len(subDir) > 0 {
    89  		subDir = utils.TrimQuoteMark(subDir)
    90  	}
    91  	if len(filename) > 0 && len(subDir) == 0 {
    92  		fmt.Println("[warn] no --sub-dir specify for --filename, the latest one will be used")
    93  	}
    94  
    95  	ctx, cancel := context.WithCancel(context.Background())
    96  	defer cancel()
    97  
    98  	resp := &pb.PurgeWorkerRelayResponse{}
    99  	err = common.SendRequest(
   100  		ctx,
   101  		"PurgeWorkerRelay",
   102  		&pb.PurgeWorkerRelayRequest{
   103  			Sources: sources,
   104  			// Inactive: inactive,
   105  			// Time:     time2.Unix(),
   106  			Filename: filename,
   107  			SubDir:   subDir,
   108  		},
   109  		&resp,
   110  	)
   111  
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	common.PrettyPrintResponse(resp)
   117  	return nil
   118  }