github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/scheduler/table_number.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 "github.com/pingcap/ticdc/cdc/model" 17 18 // TableNumberScheduler provides a feature that scheduling by the table number 19 type TableNumberScheduler struct { 20 workloads workloads 21 } 22 23 // newTableNumberScheduler creates a new table number scheduler 24 func newTableNumberScheduler() *TableNumberScheduler { 25 return &TableNumberScheduler{ 26 workloads: make(workloads), 27 } 28 } 29 30 // ResetWorkloads implements the Scheduler interface 31 func (t *TableNumberScheduler) ResetWorkloads(captureID model.CaptureID, workloads model.TaskWorkload) { 32 t.workloads.SetCapture(captureID, workloads) 33 } 34 35 // AlignCapture implements the Scheduler interface 36 func (t *TableNumberScheduler) AlignCapture(captureIDs map[model.CaptureID]struct{}) { 37 t.workloads.AlignCapture(captureIDs) 38 } 39 40 // Skewness implements the Scheduler interface 41 func (t *TableNumberScheduler) Skewness() float64 { 42 return t.workloads.Skewness() 43 } 44 45 // CalRebalanceOperates implements the Scheduler interface 46 func (t *TableNumberScheduler) CalRebalanceOperates(targetSkewness float64) ( 47 skewness float64, moveTableJobs map[model.TableID]*model.MoveTableJob) { 48 var totalTableNumber uint64 49 for _, captureWorkloads := range t.workloads { 50 totalTableNumber += uint64(len(captureWorkloads)) 51 } 52 limitTableNumber := (float64(totalTableNumber) / float64(len(t.workloads))) + 1 53 appendTables := make(map[model.TableID]model.Ts) 54 moveTableJobs = make(map[model.TableID]*model.MoveTableJob) 55 56 for captureID, captureWorkloads := range t.workloads { 57 for float64(len(captureWorkloads)) >= limitTableNumber { 58 for tableID := range captureWorkloads { 59 // find a table in this capture 60 appendTables[tableID] = 0 61 moveTableJobs[tableID] = &model.MoveTableJob{ 62 From: captureID, 63 TableID: tableID, 64 } 65 t.workloads.RemoveTable(captureID, tableID) 66 break 67 } 68 } 69 } 70 addOperations := t.DistributeTables(appendTables) 71 for captureID, tableOperations := range addOperations { 72 for tableID := range tableOperations { 73 job := moveTableJobs[tableID] 74 job.To = captureID 75 if job.From == job.To { 76 delete(moveTableJobs, tableID) 77 } 78 } 79 } 80 skewness = t.Skewness() 81 return 82 } 83 84 // DistributeTables implements the Scheduler interface 85 func (t *TableNumberScheduler) DistributeTables(tableIDs map[model.TableID]model.Ts) map[model.CaptureID]map[model.TableID]*model.TableOperation { 86 result := make(map[model.CaptureID]map[model.TableID]*model.TableOperation, len(t.workloads)) 87 for tableID, boundaryTs := range tableIDs { 88 captureID := t.workloads.SelectIdleCapture() 89 operations := result[captureID] 90 if operations == nil { 91 operations = make(map[model.TableID]*model.TableOperation) 92 result[captureID] = operations 93 } 94 operations[tableID] = &model.TableOperation{ 95 BoundaryTs: boundaryTs, 96 } 97 t.workloads.SetTable(captureID, tableID, model.WorkloadInfo{Workload: 1}) 98 } 99 return result 100 }