Skip to content

Commit

Permalink
Change task sourceID to stringer interface (#27965)
Browse files Browse the repository at this point in the history
Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Oct 26, 2023
1 parent 34b43db commit 852be15
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 98 deletions.
8 changes: 4 additions & 4 deletions internal/querycoordv2/balance/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
DistInfoPrefix = "Balance-Dists:"
)

func CreateSegmentTasksFromPlans(ctx context.Context, checkerID int64, timeout time.Duration, plans []SegmentAssignPlan) []task.Task {
func CreateSegmentTasksFromPlans(ctx context.Context, source task.Source, timeout time.Duration, plans []SegmentAssignPlan) []task.Task {
ret := make([]task.Task, 0)
for _, p := range plans {
actions := make([]task.Action, 0)
Expand All @@ -49,7 +49,7 @@ func CreateSegmentTasksFromPlans(ctx context.Context, checkerID int64, timeout t
t, err := task.NewSegmentTask(
ctx,
timeout,
checkerID,
source,
p.Segment.GetCollectionID(),
p.ReplicaID,
actions...,
Expand Down Expand Up @@ -86,7 +86,7 @@ func CreateSegmentTasksFromPlans(ctx context.Context, checkerID int64, timeout t
return ret
}

func CreateChannelTasksFromPlans(ctx context.Context, checkerID int64, timeout time.Duration, plans []ChannelAssignPlan) []task.Task {
func CreateChannelTasksFromPlans(ctx context.Context, source task.Source, timeout time.Duration, plans []ChannelAssignPlan) []task.Task {
ret := make([]task.Task, 0, len(plans))
for _, p := range plans {
actions := make([]task.Action, 0)
Expand All @@ -98,7 +98,7 @@ func CreateChannelTasksFromPlans(ctx context.Context, checkerID int64, timeout t
action := task.NewChannelAction(p.From, task.ActionTypeReduce, p.Channel.GetChannelName())
actions = append(actions, action)
}
t, err := task.NewChannelTask(ctx, timeout, checkerID, p.Channel.GetCollectionID(), p.ReplicaID, actions...)
t, err := task.NewChannelTask(ctx, timeout, source, p.Channel.GetCollectionID(), p.ReplicaID, actions...)
if err != nil {
log.Warn("create channel task failed",
zap.Int64("collection", p.Channel.GetCollectionID()),
Expand Down
5 changes: 4 additions & 1 deletion internal/querycoordv2/checkers/balance_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (

// BalanceChecker checks the cluster distribution and generates balance tasks.
type BalanceChecker struct {
baseChecker
balance.Balance
meta *meta.Meta
nodeManager *session.NodeManager
Expand All @@ -54,6 +53,10 @@ func NewBalanceChecker(meta *meta.Meta, balancer balance.Balance, nodeMgr *sessi
}
}

func (b *BalanceChecker) ID() task.Source {
return balanceChecker
}

func (b *BalanceChecker) Description() string {
return "BalanceChecker checks the cluster distribution and generates balance tasks"
}
Expand Down
5 changes: 4 additions & 1 deletion internal/querycoordv2/checkers/channel_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

// TODO(sunby): have too much similar codes with SegmentChecker
type ChannelChecker struct {
baseChecker
meta *meta.Meta
dist *meta.DistributionManager
targetMgr *meta.TargetManager
Expand All @@ -55,6 +54,10 @@ func NewChannelChecker(
}
}

func (c *ChannelChecker) ID() task.Source {
return channelChecker
}

func (c *ChannelChecker) Description() string {
return "DmChannelChecker checks the lack of DmChannels, or some DmChannels are redundant"
}
Expand Down
15 changes: 1 addition & 14 deletions internal/querycoordv2/checkers/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,7 @@ import (
)

type Checker interface {
ID() int64
SetID(id int64)
ID() task.Source
Description() string
Check(ctx context.Context) []task.Task
}

type baseChecker struct {
id int64
}

func (checker *baseChecker) ID() int64 {
return checker.id
}

func (checker *baseChecker) SetID(id int64) {
checker.id = id
}
66 changes: 40 additions & 26 deletions internal/querycoordv2/checkers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,39 @@ import (
)

const (
segmentChecker = "segment_checker"
channelChecker = "channel_checker"
balanceChecker = "balance_checker"
indexChecker = "index_checker"
segmentCheckerName = "segment_checker"
channelCheckerName = "channel_checker"
balanceCheckerName = "balance_checker"
indexCheckerName = "index_checker"
)

type checkerType int32

const (
channelChecker checkerType = iota + 1
segmentChecker
balanceChecker
indexChecker
)

var (
checkRoundTaskNumLimit = 256
checkerOrder = []string{channelChecker, segmentChecker, balanceChecker, indexChecker}
checkerOrder = []string{channelCheckerName, segmentCheckerName, balanceCheckerName, indexCheckerName}
checkerNames = map[checkerType]string{
segmentChecker: segmentCheckerName,
channelChecker: channelCheckerName,
balanceChecker: balanceCheckerName,
indexChecker: indexCheckerName,
}
)

func (s checkerType) String() string {
return checkerNames[s]
}

type CheckerController struct {
cancel context.CancelFunc
manualCheckChs map[string]chan struct{}
manualCheckChs map[checkerType]chan struct{}
meta *meta.Meta
dist *meta.DistributionManager
targetMgr *meta.TargetManager
Expand All @@ -54,7 +73,7 @@ type CheckerController struct {
balancer balance.Balance

scheduler task.Scheduler
checkers map[string]Checker
checkers map[checkerType]Checker

stopOnce sync.Once
}
Expand All @@ -70,19 +89,14 @@ func NewCheckerController(
) *CheckerController {
// CheckerController runs checkers with the order,
// the former checker has higher priority
checkers := map[string]Checker{
checkers := map[checkerType]Checker{
channelChecker: NewChannelChecker(meta, dist, targetMgr, balancer),
segmentChecker: NewSegmentChecker(meta, dist, targetMgr, balancer, nodeMgr),
balanceChecker: NewBalanceChecker(meta, balancer, nodeMgr, scheduler),
indexChecker: NewIndexChecker(meta, dist, broker),
}

id := 0
for _, checkerName := range checkerOrder {
checkers[checkerName].SetID(int64(id + 1))
}

manualCheckChs := map[string]chan struct{}{
manualCheckChs := map[checkerType]chan struct{}{
channelChecker: make(chan struct{}, 1),
segmentChecker: make(chan struct{}, 1),
balanceChecker: make(chan struct{}, 1),
Expand All @@ -103,13 +117,13 @@ func (controller *CheckerController) Start() {
ctx, cancel := context.WithCancel(context.Background())
controller.cancel = cancel

for checkerType := range controller.checkers {
go controller.startChecker(ctx, checkerType)
for checker := range controller.checkers {
go controller.startChecker(ctx, checker)
}
}

func getCheckerInterval(checkerType string) time.Duration {
switch checkerType {
func getCheckerInterval(checker checkerType) time.Duration {
switch checker {
case segmentChecker:
return Params.QueryCoordCfg.SegmentCheckInterval.GetAsDuration(time.Millisecond)
case channelChecker:
Expand All @@ -123,24 +137,24 @@ func getCheckerInterval(checkerType string) time.Duration {
}
}

func (controller *CheckerController) startChecker(ctx context.Context, checkerType string) {
interval := getCheckerInterval(checkerType)
func (controller *CheckerController) startChecker(ctx context.Context, checker checkerType) {
interval := getCheckerInterval(checker)
ticker := time.NewTicker(interval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
log.Info("Checker stopped",
zap.String("type", checkerType))
zap.String("type", checker.String()))
return

case <-ticker.C:
controller.check(ctx, checkerType)
controller.check(ctx, checker)

case <-controller.manualCheckChs[checkerType]:
case <-controller.manualCheckChs[checker]:
ticker.Stop()
controller.check(ctx, checkerType)
controller.check(ctx, checker)
ticker.Reset(interval)
}
}
Expand All @@ -164,8 +178,8 @@ func (controller *CheckerController) Check() {
}

// check is the real implementation of Check
func (controller *CheckerController) check(ctx context.Context, checkerType string) {
checker := controller.checkers[checkerType]
func (controller *CheckerController) check(ctx context.Context, checkType checkerType) {
checker := controller.checkers[checkType]
tasks := checker.Check(ctx)

for _, task := range tasks {
Expand Down
5 changes: 4 additions & 1 deletion internal/querycoordv2/checkers/index_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var _ Checker = (*IndexChecker)(nil)

// IndexChecker perform segment index check.
type IndexChecker struct {
baseChecker
meta *meta.Meta
dist *meta.DistributionManager
broker meta.Broker
Expand All @@ -53,6 +52,10 @@ func NewIndexChecker(
}
}

func (c *IndexChecker) ID() task.Source {
return indexChecker
}

func (c *IndexChecker) Description() string {
return "SegmentChecker checks index state change of segments and generates load index task"
}
Expand Down
5 changes: 4 additions & 1 deletion internal/querycoordv2/checkers/segment_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
)

type SegmentChecker struct {
baseChecker
meta *meta.Meta
dist *meta.DistributionManager
targetMgr *meta.TargetManager
Expand All @@ -60,6 +59,10 @@ func NewSegmentChecker(
}
}

func (c *SegmentChecker) ID() task.Source {
return segmentChecker
}

func (c *SegmentChecker) Description() string {
return "SegmentChecker checks the lack of segments, or some segments are redundant"
}
Expand Down
2 changes: 1 addition & 1 deletion internal/querycoordv2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *Server) balanceSegments(ctx context.Context, req *querypb.LoadBalanceRe
)
task, err := task.NewSegmentTask(ctx,
Params.QueryCoordCfg.SegmentTaskTimeout.GetAsDuration(time.Millisecond),
req.GetBase().GetMsgID(),
task.WrapIDSource(req.GetBase().GetMsgID()),
req.GetCollectionID(),
replica.GetID(),
task.NewSegmentActionWithScope(plan.To, task.ActionTypeGrow, plan.Segment.GetInsertChannel(), plan.Segment.GetID(), querypb.DataScope_Historical),
Expand Down
12 changes: 6 additions & 6 deletions internal/querycoordv2/task/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (ex *Executor) Execute(task Task, step int) bool {
zap.Int64("collectionID", task.CollectionID()),
zap.Int64("replicaID", task.ReplicaID()),
zap.Int("step", step),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

go func() {
Expand Down Expand Up @@ -169,7 +169,7 @@ func (ex *Executor) processMergeTask(mergeTask *LoadSegmentsTask) {
zap.String("shard", task.Shard()),
zap.Int64s("segmentIDs", segments),
zap.Int64("nodeID", action.Node()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

// Get shard leader for the given replica and segment
Expand Down Expand Up @@ -231,7 +231,7 @@ func (ex *Executor) loadSegment(task *SegmentTask, step int) error {
zap.Int64("replicaID", task.ReplicaID()),
zap.Int64("segmentID", task.segmentID),
zap.Int64("node", action.Node()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

var err error
Expand Down Expand Up @@ -312,7 +312,7 @@ func (ex *Executor) releaseSegment(task *SegmentTask, step int) {
zap.Int64("replicaID", task.ReplicaID()),
zap.Int64("segmentID", task.segmentID),
zap.Int64("node", action.Node()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

ctx := task.Context()
Expand Down Expand Up @@ -384,7 +384,7 @@ func (ex *Executor) subDmChannel(task *ChannelTask, step int) error {
zap.Int64("replicaID", task.ReplicaID()),
zap.String("channel", task.Channel()),
zap.Int64("node", action.Node()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

var err error
Expand Down Expand Up @@ -467,7 +467,7 @@ func (ex *Executor) unsubDmChannel(task *ChannelTask, step int) error {
zap.Int64("replicaID", task.ReplicaID()),
zap.String("channel", task.Channel()),
zap.Int64("node", action.Node()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

var err error
Expand Down
2 changes: 1 addition & 1 deletion internal/querycoordv2/task/merger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (suite *MergerSuite) TestMerge() {
ctx := context.Background()

for segmentID := int64(1); segmentID <= 3; segmentID++ {
task, err := NewSegmentTask(ctx, timeout, 0, suite.collectionID, suite.replicaID,
task, err := NewSegmentTask(ctx, timeout, WrapIDSource(0), suite.collectionID, suite.replicaID,
NewSegmentAction(suite.nodeID, ActionTypeGrow, "", segmentID))
suite.NoError(err)
suite.merger.Add(NewLoadSegmentsTask(task, 0, suite.requests[segmentID]))
Expand Down
10 changes: 5 additions & 5 deletions internal/querycoordv2/task/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (scheduler *taskScheduler) promote(task Task) error {
zap.Int64("taskID", task.ID()),
zap.Int64("collectionID", task.CollectionID()),
zap.Int64("replicaID", task.ReplicaID()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

if err := scheduler.check(task); err != nil {
Expand Down Expand Up @@ -643,7 +643,7 @@ func (scheduler *taskScheduler) process(task Task) bool {
zap.Int64("collectionID", task.CollectionID()),
zap.Int64("replicaID", task.ReplicaID()),
zap.String("type", GetTaskType(task).String()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

actions, step := task.Actions(), task.Step()
Expand Down Expand Up @@ -733,7 +733,7 @@ func (scheduler *taskScheduler) checkStale(task Task) error {
zap.Int64("taskID", task.ID()),
zap.Int64("collectionID", task.CollectionID()),
zap.Int64("replicaID", task.ReplicaID()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

switch task := task.(type) {
Expand Down Expand Up @@ -770,7 +770,7 @@ func (scheduler *taskScheduler) checkSegmentTaskStale(task *SegmentTask) error {
zap.Int64("taskID", task.ID()),
zap.Int64("collectionID", task.CollectionID()),
zap.Int64("replicaID", task.ReplicaID()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

for _, action := range task.Actions() {
Expand Down Expand Up @@ -814,7 +814,7 @@ func (scheduler *taskScheduler) checkChannelTaskStale(task *ChannelTask) error {
zap.Int64("taskID", task.ID()),
zap.Int64("collectionID", task.CollectionID()),
zap.Int64("replicaID", task.ReplicaID()),
zap.Int64("source", task.SourceID()),
zap.String("source", task.Source().String()),
)

for _, action := range task.Actions() {
Expand Down
Loading

0 comments on commit 852be15

Please sign in to comment.