github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/servermaster/scheduler/scheduler.go (about)

     1  // Copyright 2022 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  	"context"
    18  	"math/rand"
    19  
    20  	"github.com/pingcap/tiflow/engine/model"
    21  	schedModel "github.com/pingcap/tiflow/engine/servermaster/scheduler/model"
    22  	"github.com/pingcap/tiflow/pkg/errors"
    23  )
    24  
    25  // Scheduler is a full set of scheduling management, containing capacity provider,
    26  // real scheduler and resource placement manager.
    27  type Scheduler struct {
    28  	infoProvider executorInfoProvider
    29  	filters      []filter
    30  }
    31  
    32  // NewScheduler creates a new Scheduler instance
    33  func NewScheduler(
    34  	infoProvider executorInfoProvider,
    35  	placementConstrainer PlacementConstrainer,
    36  ) *Scheduler {
    37  	return &Scheduler{
    38  		infoProvider: infoProvider,
    39  		filters: []filter{
    40  			newResourceFilter(placementConstrainer),
    41  			newSelectorFilter(infoProvider),
    42  		},
    43  	}
    44  }
    45  
    46  // ScheduleTask tries to assign an executor to a given task.
    47  func (s *Scheduler) ScheduleTask(
    48  	ctx context.Context,
    49  	request *schedModel.SchedulerRequest,
    50  ) (*schedModel.SchedulerResponse, error) {
    51  	candidates, err := s.chainFilter(ctx, request)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	if len(candidates) == 0 {
    56  		return nil, errors.ErrNoQualifiedExecutor.GenWithStackByArgs()
    57  	}
    58  	executorID := candidates[rand.Intn(len(candidates))]
    59  	return &schedModel.SchedulerResponse{ExecutorID: executorID}, nil
    60  }
    61  
    62  // chainFilter runs the filter chain and returns a final candidate list.
    63  func (s *Scheduler) chainFilter(
    64  	ctx context.Context,
    65  	request *schedModel.SchedulerRequest,
    66  ) (candidates []model.ExecutorID, retErr error) {
    67  	infos := s.infoProvider.GetExecutorInfos()
    68  	for id := range infos {
    69  		candidates = append(candidates, id)
    70  	}
    71  	for _, filter := range s.filters {
    72  		var err error
    73  		candidates, err = filter.GetEligibleExecutors(ctx, request, candidates)
    74  		if err != nil {
    75  			// Note: filters are guaranteed to return errors when no suitable candidate is found.
    76  			return nil, err
    77  		}
    78  	}
    79  	return
    80  }