github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/ha/ops_test.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 ha
    15  
    16  import (
    17  	. "github.com/pingcap/check"
    18  	"github.com/pingcap/tiflow/dm/config"
    19  	"github.com/pingcap/tiflow/dm/pb"
    20  )
    21  
    22  func (t *testForEtcd) TestOpsEtcd(c *C) {
    23  	defer clearTestInfoOperation(c)
    24  
    25  	var (
    26  		source         = "mysql-replica-1"
    27  		worker         = "dm-worker-1"
    28  		task1          = "task-1"
    29  		task2          = "task-2"
    30  		relayStage     = NewRelayStage(pb.Stage_Running, source)
    31  		subtaskStage1  = NewSubTaskStage(pb.Stage_Running, source, task1)
    32  		subtaskStage2  = NewSubTaskStage(pb.Stage_Running, source, task2)
    33  		validatorStage = NewSubTaskStage(pb.Stage_Running, source, task2)
    34  		bound          = NewSourceBound(source, worker)
    35  
    36  		emptyStage  Stage
    37  		subtaskCfg1 config.SubTaskConfig
    38  	)
    39  
    40  	sourceCfg, err := config.LoadFromFile(sourceSampleFilePath)
    41  	c.Assert(err, IsNil)
    42  	sourceCfg.SourceID = source
    43  	c.Assert(subtaskCfg1.Decode(config.SampleSubtaskConfig, true), IsNil)
    44  	subtaskCfg1.SourceID = source
    45  	subtaskCfg1.Name = task1
    46  	c.Assert(subtaskCfg1.Adjust(true), IsNil)
    47  	subtaskCfg2 := subtaskCfg1
    48  	subtaskCfg2.Name = task2
    49  	c.Assert(subtaskCfg2.Adjust(true), IsNil)
    50  
    51  	// put relay stage and source bound.
    52  	rev1, err := PutRelayStageRelayConfigSourceBound(etcdTestCli, relayStage, bound)
    53  	c.Assert(err, IsNil)
    54  	c.Assert(rev1, Greater, int64(0))
    55  	// put source config.
    56  	rev2, err := PutSourceCfg(etcdTestCli, sourceCfg)
    57  	c.Assert(err, IsNil)
    58  	c.Assert(rev2, Greater, rev1)
    59  
    60  	// get them back.
    61  	st1, rev3, err := GetRelayStage(etcdTestCli, source)
    62  	c.Assert(err, IsNil)
    63  	c.Assert(rev3, Equals, rev2)
    64  	relayStage.Revision = rev1
    65  	c.Assert(st1, DeepEquals, relayStage)
    66  	sbm1, rev3, err := GetSourceBound(etcdTestCli, worker)
    67  	c.Assert(err, IsNil)
    68  	c.Assert(rev3, Equals, rev2)
    69  	c.Assert(sbm1, HasLen, 1)
    70  	bound.Revision = rev1
    71  	c.Assert(sbm1[worker], DeepEquals, bound)
    72  	scm1, rev3, err := GetSourceCfg(etcdTestCli, source, 0)
    73  	c.Assert(err, IsNil)
    74  	c.Assert(rev3, Equals, rev2)
    75  	soCfg1 := scm1[source]
    76  	c.Assert(soCfg1, DeepEquals, sourceCfg)
    77  
    78  	// delete source config, relay stage and source bound.
    79  	rev4, err := DeleteSourceCfgRelayStageSourceBound(etcdTestCli, source, worker)
    80  	c.Assert(err, IsNil)
    81  	c.Assert(rev4, Greater, rev3)
    82  
    83  	// try to get them back again.
    84  	st2, rev5, err := GetRelayStage(etcdTestCli, source)
    85  	c.Assert(err, IsNil)
    86  	c.Assert(rev5, Equals, rev4)
    87  	c.Assert(st2, Equals, emptyStage)
    88  	sbm2, rev5, err := GetSourceBound(etcdTestCli, worker)
    89  	c.Assert(err, IsNil)
    90  	c.Assert(rev5, Equals, rev4)
    91  	c.Assert(sbm2, HasLen, 0)
    92  	scm2, rev5, err := GetSourceCfg(etcdTestCli, source, 0)
    93  	c.Assert(err, IsNil)
    94  	c.Assert(rev5, Equals, rev4)
    95  	c.Assert(scm2, HasLen, 0)
    96  
    97  	// put subtask config and subtask stage.
    98  	rev6, err := PutSubTaskCfgStage(etcdTestCli, []config.SubTaskConfig{subtaskCfg1, subtaskCfg2}, []Stage{subtaskStage1, subtaskStage2}, []Stage{validatorStage})
    99  	c.Assert(err, IsNil)
   100  	c.Assert(rev6, Greater, rev5)
   101  
   102  	// get them back.
   103  	stcm, rev7, err := GetSubTaskCfg(etcdTestCli, source, "", 0)
   104  	c.Assert(err, IsNil)
   105  	c.Assert(rev7, Equals, rev6)
   106  	c.Assert(stcm, HasLen, 2)
   107  	c.Assert(stcm[task1], DeepEquals, subtaskCfg1)
   108  	c.Assert(stcm[task2], DeepEquals, subtaskCfg2)
   109  	stsm, rev7, err := GetSubTaskStage(etcdTestCli, source, "")
   110  	c.Assert(err, IsNil)
   111  	c.Assert(rev7, Equals, rev6)
   112  	c.Assert(stsm, HasLen, 2)
   113  	subtaskStage1.Revision = rev6
   114  	subtaskStage2.Revision = rev6
   115  	c.Assert(stsm[task1], DeepEquals, subtaskStage1)
   116  	c.Assert(stsm[task2], DeepEquals, subtaskStage2)
   117  	validatorStages, rev7, err := GetValidatorStage(etcdTestCli, source, "", rev6)
   118  	c.Assert(err, IsNil)
   119  	c.Assert(rev7, Equals, rev6)
   120  	c.Assert(validatorStages, HasLen, 1)
   121  	validatorStage.Revision = rev6
   122  	c.Assert(validatorStages[task2], DeepEquals, validatorStage)
   123  	// get with task name
   124  	validatorStages, rev7, err = GetValidatorStage(etcdTestCli, source, task2, rev6)
   125  	c.Assert(err, IsNil)
   126  	c.Assert(rev7, Equals, rev6)
   127  	c.Assert(validatorStages, HasLen, 1)
   128  	validatorStage.Revision = rev6
   129  	c.Assert(validatorStages[task2], DeepEquals, validatorStage)
   130  
   131  	// delete them.
   132  	rev8, err := DeleteSubTaskCfgStage(etcdTestCli, []config.SubTaskConfig{subtaskCfg1, subtaskCfg2}, []Stage{subtaskStage1, subtaskStage2}, []Stage{validatorStage})
   133  	c.Assert(err, IsNil)
   134  	c.Assert(rev8, Greater, rev7)
   135  
   136  	// try to get them back again.
   137  	stcm, rev9, err := GetSubTaskCfg(etcdTestCli, source, "", 0)
   138  	c.Assert(err, IsNil)
   139  	c.Assert(rev9, Equals, rev8)
   140  	c.Assert(stcm, HasLen, 0)
   141  	stsm, rev9, err = GetSubTaskStage(etcdTestCli, source, "")
   142  	c.Assert(err, IsNil)
   143  	c.Assert(rev9, Equals, rev8)
   144  	c.Assert(stsm, HasLen, 0)
   145  	validatorStages, rev9, err = GetValidatorStage(etcdTestCli, source, "", 0)
   146  	c.Assert(err, IsNil)
   147  	c.Assert(rev9, Equals, rev8)
   148  	c.Assert(validatorStages, HasLen, 0)
   149  }