github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/scheduler/table_number_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 scheduler
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/pingcap/ticdc/cdc/model"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  
    22  	"github.com/pingcap/check"
    23  )
    24  
    25  type tableNumberSuite struct{}
    26  
    27  var _ = check.Suite(&tableNumberSuite{})
    28  
    29  func (s *tableNumberSuite) TestDistributeTables(c *check.C) {
    30  	defer testleak.AfterTest(c)()
    31  	scheduler := newTableNumberScheduler()
    32  	scheduler.ResetWorkloads("capture1", model.TaskWorkload{
    33  		1: model.WorkloadInfo{Workload: 1},
    34  		2: model.WorkloadInfo{Workload: 1},
    35  	})
    36  	scheduler.ResetWorkloads("capture2", model.TaskWorkload{
    37  		3: model.WorkloadInfo{Workload: 1},
    38  		4: model.WorkloadInfo{Workload: 1},
    39  	})
    40  	scheduler.ResetWorkloads("capture3", model.TaskWorkload{
    41  		5: model.WorkloadInfo{Workload: 1},
    42  		6: model.WorkloadInfo{Workload: 1},
    43  		7: model.WorkloadInfo{Workload: 1},
    44  		8: model.WorkloadInfo{Workload: 1},
    45  	})
    46  	c.Assert(fmt.Sprintf("%.2f%%", scheduler.Skewness()*100), check.Equals, "35.36%")
    47  	tableToAdd := map[model.TableID]model.Ts{10: 1, 11: 2, 12: 3, 13: 4, 14: 5, 15: 6, 16: 7, 17: 8}
    48  	result := scheduler.DistributeTables(tableToAdd)
    49  	c.Assert(len(result), check.Equals, 3) // there three captures
    50  	totalTableNum := 0
    51  	for _, ops := range result {
    52  		for tableID, op := range ops {
    53  			ts, exist := tableToAdd[tableID]
    54  			c.Assert(exist, check.IsTrue)
    55  			c.Assert(op.Delete, check.IsFalse)
    56  			c.Assert(op.BoundaryTs, check.Equals, ts)
    57  			c.Assert(op.Done, check.IsFalse)
    58  			totalTableNum++
    59  		}
    60  	}
    61  	c.Assert(totalTableNum, check.Equals, 8) // there eight tables to add
    62  	c.Assert(fmt.Sprintf("%.2f%%", scheduler.Skewness()*100), check.Equals, "8.84%")
    63  }
    64  
    65  func (s *tableNumberSuite) TestCalRebalanceOperates(c *check.C) {
    66  	defer testleak.AfterTest(c)()
    67  	scheduler := newTableNumberScheduler()
    68  	scheduler.ResetWorkloads("capture1", model.TaskWorkload{
    69  		1: model.WorkloadInfo{Workload: 1},
    70  		2: model.WorkloadInfo{Workload: 1},
    71  	})
    72  	scheduler.ResetWorkloads("capture2", model.TaskWorkload{
    73  		3: model.WorkloadInfo{Workload: 1},
    74  		4: model.WorkloadInfo{Workload: 1},
    75  	})
    76  	scheduler.ResetWorkloads("capture3", model.TaskWorkload{
    77  		5:  model.WorkloadInfo{Workload: 1},
    78  		6:  model.WorkloadInfo{Workload: 1},
    79  		7:  model.WorkloadInfo{Workload: 1},
    80  		8:  model.WorkloadInfo{Workload: 1},
    81  		9:  model.WorkloadInfo{Workload: 1},
    82  		10: model.WorkloadInfo{Workload: 1},
    83  	})
    84  	c.Assert(fmt.Sprintf("%.2f%%", scheduler.Skewness()*100), check.Equals, "56.57%")
    85  	skewness, moveJobs := scheduler.CalRebalanceOperates(0)
    86  
    87  	for tableID, job := range moveJobs {
    88  		c.Assert(len(job.From), check.Greater, 0)
    89  		c.Assert(len(job.To), check.Greater, 0)
    90  		c.Assert(job.TableID, check.Equals, tableID)
    91  		c.Assert(job.From, check.Not(check.Equals), job.To)
    92  		c.Assert(job.Status, check.Equals, model.MoveTableStatusNone)
    93  	}
    94  
    95  	c.Assert(fmt.Sprintf("%.2f%%", skewness*100), check.Equals, "14.14%")
    96  
    97  	scheduler.ResetWorkloads("capture1", model.TaskWorkload{
    98  		1: model.WorkloadInfo{Workload: 1},
    99  		2: model.WorkloadInfo{Workload: 1},
   100  		3: model.WorkloadInfo{Workload: 1},
   101  	})
   102  	scheduler.ResetWorkloads("capture2", model.TaskWorkload{})
   103  	scheduler.ResetWorkloads("capture3", model.TaskWorkload{})
   104  	c.Assert(fmt.Sprintf("%.2f%%", scheduler.Skewness()*100), check.Equals, "141.42%")
   105  	skewness, moveJobs = scheduler.CalRebalanceOperates(0)
   106  
   107  	for tableID, job := range moveJobs {
   108  		c.Assert(len(job.From), check.Greater, 0)
   109  		c.Assert(len(job.To), check.Greater, 0)
   110  		c.Assert(job.TableID, check.Equals, tableID)
   111  		c.Assert(job.From, check.Not(check.Equals), job.To)
   112  		c.Assert(job.Status, check.Equals, model.MoveTableStatusNone)
   113  	}
   114  	c.Assert(fmt.Sprintf("%.2f%%", skewness*100), check.Equals, "0.00%")
   115  }