github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/chaos/cases/source.go (about)

     1  // Copyright 2020 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 main
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  	"time"
    23  
    24  	config2 "github.com/pingcap/tiflow/dm/config"
    25  	"github.com/pingcap/tiflow/dm/pb"
    26  )
    27  
    28  // createSources does `operate-source create` operation for two sources.
    29  // NOTE: we put source config files in `conf` directory.
    30  func createSources(ctx context.Context, cli pb.MasterClient, cfg *config) error {
    31  	s1Path := filepath.Join(cfg.ConfigDir, "source1.yaml")
    32  	s2Path := filepath.Join(cfg.ConfigDir, "source2.yaml")
    33  	s3Path := filepath.Join(cfg.ConfigDir, "source3.yaml")
    34  
    35  	s1Content, err := os.ReadFile(s1Path)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	s2Content, err := os.ReadFile(s2Path)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	s3Content, err := os.ReadFile(s3Path)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	cfg1, err := config2.SourceCfgFromYaml(string(s1Content))
    49  	if err != nil {
    50  		return err
    51  	}
    52  	cfg2, err := config2.SourceCfgFromYaml(string(s2Content))
    53  	if err != nil {
    54  		return err
    55  	}
    56  	cfg3, err := config2.SourceCfgFromYaml(string(s3Content))
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// replace DB config.
    62  	cfg1.From = cfg.Source1
    63  	cfg2.From = cfg.Source2
    64  	cfg3.From = cfg.Source3
    65  
    66  	// reduce backoffmax for autoresume
    67  	cfg1.Checker.BackoffMax = config2.Duration{Duration: 5 * time.Second}
    68  	cfg2.Checker.BackoffMax = config2.Duration{Duration: 5 * time.Second}
    69  	cfg3.Checker.BackoffMax = config2.Duration{Duration: 5 * time.Second}
    70  
    71  	s1Content2, err := cfg1.Yaml()
    72  	if err != nil {
    73  		return err
    74  	}
    75  	s2Content2, err := cfg2.Yaml()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	s3Content3, err := cfg3.Yaml()
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	resp, err := cli.OperateSource(ctx, &pb.OperateSourceRequest{
    85  		Op:     pb.SourceOp_StartSource,
    86  		Config: []string{s1Content2, s2Content2, s3Content3},
    87  	})
    88  	if err != nil {
    89  		return err
    90  	} else if !resp.Result && !strings.Contains(resp.Msg, "already exists") { // imprecise match
    91  		return fmt.Errorf("fail to create source: %s", resp.Msg)
    92  	}
    93  	return nil
    94  }