github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_changefeed_helper.go (about)

     1  // Copyright 2021 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 cli
    15  
    16  import (
    17  	"bufio"
    18  	"fmt"
    19  	"os"
    20  	"strings"
    21  	"time"
    22  
    23  	cerror "github.com/pingcap/tiflow/pkg/errors"
    24  	"github.com/spf13/cobra"
    25  	"github.com/tikv/client-go/v2/oracle"
    26  )
    27  
    28  const (
    29  	// tsGapWarning specifies the OOM threshold.
    30  	// 1 day in milliseconds
    31  	tsGapWarning = 86400 * 1000
    32  )
    33  
    34  func readInput() (string, error) {
    35  	reader := bufio.NewReader(os.Stdin)
    36  	msg, err := reader.ReadString('\n')
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  	return strings.TrimSpace(msg), nil
    41  }
    42  
    43  func readYOrN(cmd *cobra.Command) bool {
    44  	var yOrN string
    45  	_, err := fmt.Scan(&yOrN)
    46  	if err != nil {
    47  		cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error())
    48  		return false
    49  	}
    50  	if strings.ToLower(strings.TrimSpace(yOrN)) != "y" {
    51  		return false
    52  	}
    53  	return true
    54  }
    55  
    56  // confirmLargeDataGap checks if a large data gap is used.
    57  func confirmLargeDataGap(cmd *cobra.Command, currentPhysical int64, startTs uint64, command string) error {
    58  	tsGap := currentPhysical - oracle.ExtractPhysical(startTs)
    59  
    60  	if tsGap > tsGapWarning {
    61  		cmd.Printf("Replicate lag (%s) is larger than 1 days, "+
    62  			"large data may cause OOM, confirm to continue at your own risk [Y/N]\n",
    63  			time.Duration(tsGap)*time.Millisecond,
    64  		)
    65  		confirmed := readYOrN(cmd)
    66  		if !confirmed {
    67  			cmd.Printf("Abort changefeed %s.\n", command)
    68  			return cerror.ErrCliAborted.FastGenByArgs(fmt.Sprintf("cli changefeed %s", command))
    69  		}
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // confirmOverwriteCheckpointTs prompts risk warnings when users are trying to
    76  // overwrite the checkpointTs
    77  func confirmOverwriteCheckpointTs(
    78  	cmd *cobra.Command, changefeedID string, checkpointTs uint64,
    79  ) error {
    80  	cmd.Printf("You are overwriting the checkpoint of changefeed(%s) to %d,"+
    81  		" which may lead to data loss or data duplication.\nConfirm that you know"+
    82  		" what this command will do and use it at your own risk [Y/N]", changefeedID, checkpointTs)
    83  	confirmed := readYOrN(cmd)
    84  	if !confirmed {
    85  		cmd.Printf("Abort changefeed resume.\n")
    86  		return cerror.ErrCliAborted.FastGenByArgs("cli changefeed resume")
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  // confirmIgnoreIneligibleTables confirm if user need to ignore ineligible tables.
    93  // If ignore it will return true.
    94  func confirmIgnoreIneligibleTables(cmd *cobra.Command) (bool, error) {
    95  	cmd.Printf("Could you agree to ignore those tables, and continue to replicate [Y/N]\n" +
    96  		"Note: If you don't want to ignore those tables, please set `force-replicate to` true " +
    97  		"in the changefeed config file.\n")
    98  	confirmed := readYOrN(cmd)
    99  	if !confirmed {
   100  		cmd.Printf("No changefeed is created because you don't want to ignore some tables.\n")
   101  		return false, cerror.ErrCliAborted.FastGenByArgs("cli changefeed create")
   102  	}
   103  
   104  	return true, nil
   105  }