|
| 1 | +// Copyright 2024 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 checker |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/flowbehappy/tigate/maintainer/operator" |
| 21 | + "github.com/flowbehappy/tigate/maintainer/replica" |
| 22 | + "github.com/flowbehappy/tigate/maintainer/split" |
| 23 | + "github.com/flowbehappy/tigate/server/watcher" |
| 24 | + "github.com/pingcap/log" |
| 25 | + "go.uber.org/zap" |
| 26 | +) |
| 27 | + |
| 28 | +// SplitChecker is used to check the split status of all spans |
| 29 | +type SplitChecker struct { |
| 30 | + changefeedID string |
| 31 | + splitter *split.Splitter |
| 32 | + opController *operator.Controller |
| 33 | + db *replica.ReplicationDB |
| 34 | + nodeManager *watcher.NodeManager |
| 35 | + |
| 36 | + maxCheckTime time.Duration |
| 37 | + checkInterval time.Duration |
| 38 | + lastCheckTime time.Time |
| 39 | + |
| 40 | + checkedIndex int |
| 41 | + cachedSpans []*replica.SpanReplication |
| 42 | +} |
| 43 | + |
| 44 | +func NewSplitChecker( |
| 45 | + changefeedID string, |
| 46 | + splitter *split.Splitter, |
| 47 | + opController *operator.Controller, |
| 48 | + db *replica.ReplicationDB, |
| 49 | + nodeManager *watcher.NodeManager) *SplitChecker { |
| 50 | + return &SplitChecker{ |
| 51 | + changefeedID: changefeedID, |
| 52 | + splitter: splitter, |
| 53 | + opController: opController, |
| 54 | + db: db, |
| 55 | + nodeManager: nodeManager, |
| 56 | + |
| 57 | + maxCheckTime: time.Second * 5, |
| 58 | + checkInterval: time.Second * 120, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (s *SplitChecker) Name() string { |
| 63 | + return "split-checker" |
| 64 | +} |
| 65 | + |
| 66 | +func (s *SplitChecker) Check() { |
| 67 | + if s.splitter == nil { |
| 68 | + return |
| 69 | + } |
| 70 | + if time.Since(s.lastCheckTime) < s.checkInterval { |
| 71 | + return |
| 72 | + } |
| 73 | + if s.cachedSpans == nil { |
| 74 | + s.cachedSpans = s.db.GetReplicating() |
| 75 | + s.checkedIndex = 0 |
| 76 | + } |
| 77 | + start := time.Now() |
| 78 | + for ; s.checkedIndex < len(s.cachedSpans); s.checkedIndex++ { |
| 79 | + span := s.cachedSpans[s.checkedIndex] |
| 80 | + if s.db.GetTaskByID(span.ID) == nil { |
| 81 | + continue |
| 82 | + } |
| 83 | + spans := s.splitter.SplitSpans(context.Background(), span.Span, len(s.nodeManager.GetAliveNodes())) |
| 84 | + if len(spans) > 1 { |
| 85 | + log.Info("split span", |
| 86 | + zap.String("changefeed", s.changefeedID), |
| 87 | + zap.String("span", span.ID.String()), |
| 88 | + zap.Int("span szie", len(spans))) |
| 89 | + s.opController.AddOperator(operator.NewSplitDispatcherOperator(s.db, span, span.GetNodeID(), spans)) |
| 90 | + } |
| 91 | + if time.Since(start) > s.maxCheckTime { |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + if s.checkedIndex >= len(s.cachedSpans) { |
| 96 | + s.cachedSpans = nil |
| 97 | + s.checkedIndex = 0 |
| 98 | + s.lastCheckTime = time.Now() |
| 99 | + } |
| 100 | +} |
0 commit comments