github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/ha/subtask_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  	"context"
    18  
    19  	. "github.com/pingcap/check"
    20  	"github.com/pingcap/tiflow/dm/config"
    21  )
    22  
    23  func (t *testForEtcd) TestSubTaskEtcd(c *C) {
    24  	defer clearTestInfoOperation(c)
    25  
    26  	cfg1 := config.SubTaskConfig{}
    27  	c.Assert(cfg1.Decode(config.SampleSubtaskConfig, true), IsNil)
    28  	source := cfg1.SourceID
    29  	taskName1 := cfg1.Name
    30  
    31  	taskName2 := taskName1 + "2"
    32  	cfg2 := cfg1
    33  	cfg2.Name = taskName2
    34  	err := cfg2.Adjust(true)
    35  	c.Assert(err, IsNil)
    36  
    37  	// no subtask config exist.
    38  	tsm1, rev1, err := GetSubTaskCfg(etcdTestCli, source, taskName1, 0)
    39  	c.Assert(err, IsNil)
    40  	c.Assert(rev1, Greater, int64(0))
    41  	c.Assert(tsm1, HasLen, 0)
    42  
    43  	// put subtask configs.
    44  	rev2, err := PutSubTaskCfgStage(etcdTestCli, []config.SubTaskConfig{cfg1, cfg2}, []Stage{}, nil)
    45  	c.Assert(err, IsNil)
    46  	c.Assert(rev2, Greater, rev1)
    47  
    48  	// get single config back.
    49  	tsm2, rev3, err := GetSubTaskCfg(etcdTestCli, source, taskName1, 0)
    50  	c.Assert(err, IsNil)
    51  	c.Assert(rev3, Equals, rev2)
    52  	c.Assert(tsm2, HasLen, 1)
    53  	c.Assert(tsm2, HasKey, taskName1)
    54  	c.Assert(tsm2[taskName1], DeepEquals, cfg1)
    55  
    56  	tsm3, rev4, err := GetSubTaskCfg(etcdTestCli, source, "", 0)
    57  	c.Assert(err, IsNil)
    58  	c.Assert(rev4, Equals, rev3)
    59  	c.Assert(tsm3, HasLen, 2)
    60  	c.Assert(tsm3, HasKey, taskName1)
    61  	c.Assert(tsm3, HasKey, taskName2)
    62  	c.Assert(tsm3[taskName1], DeepEquals, cfg1)
    63  	c.Assert(tsm3[taskName2], DeepEquals, cfg2)
    64  
    65  	// get all subtask configs.
    66  	stmm, rev4, err := GetAllSubTaskCfg(etcdTestCli)
    67  	c.Assert(err, IsNil)
    68  	c.Assert(rev4, Equals, rev3)
    69  	c.Assert(stmm, HasLen, 1)
    70  	c.Assert(stmm[source], HasLen, 2)
    71  	c.Assert(stmm[source][taskName1], DeepEquals, cfg1)
    72  	c.Assert(stmm[source][taskName2], DeepEquals, cfg2)
    73  
    74  	// delete the config.
    75  	deleteOps := deleteSubTaskCfgOp(cfg1)
    76  	_, err = etcdTestCli.Txn(context.Background()).Then(deleteOps...).Commit()
    77  	c.Assert(err, IsNil)
    78  	deleteOps = deleteSubTaskCfgOp(cfg2)
    79  	deleteResp, err := etcdTestCli.Txn(context.Background()).Then(deleteOps...).Commit()
    80  	c.Assert(err, IsNil)
    81  
    82  	// get again, not exists now.
    83  	tsm4, rev5, err := GetSubTaskCfg(etcdTestCli, source, taskName1, 0)
    84  	c.Assert(err, IsNil)
    85  	c.Assert(rev5, Equals, deleteResp.Header.Revision)
    86  	c.Assert(tsm4, HasLen, 0)
    87  
    88  	// put subtask config.
    89  	rev6, err := PutSubTaskCfgStage(etcdTestCli, []config.SubTaskConfig{cfg1}, []Stage{}, nil)
    90  	c.Assert(err, IsNil)
    91  	c.Assert(rev6, Greater, int64(0))
    92  
    93  	// update subtask config.
    94  	cfg3 := cfg1
    95  	cfg3.SourceID = "testForRevision"
    96  	rev7, err := PutSubTaskCfgStage(etcdTestCli, []config.SubTaskConfig{cfg3}, []Stage{}, nil)
    97  	c.Assert(err, IsNil)
    98  	c.Assert(rev7, Greater, rev6)
    99  
   100  	// get subtask from rev6. shoule be equal to cfg1
   101  	tsm5, rev8, err := GetSubTaskCfg(etcdTestCli, source, taskName1, rev6)
   102  	c.Assert(err, IsNil)
   103  	c.Assert(rev8, Equals, rev7)
   104  	c.Assert(tsm5, HasLen, 1)
   105  	c.Assert(tsm5, HasKey, taskName1)
   106  	c.Assert(tsm5[taskName1], DeepEquals, cfg1)
   107  }