2024-01-31 20:45:04 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package importv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/conc"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
|
|
|
)
|
|
|
|
|
2024-04-01 20:09:13 +08:00
|
|
|
type Scheduler interface {
|
2024-01-31 20:45:04 +08:00
|
|
|
Start()
|
|
|
|
Slots() int64
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:09:13 +08:00
|
|
|
type scheduler struct {
|
2024-01-31 20:45:04 +08:00
|
|
|
manager TaskManager
|
|
|
|
|
|
|
|
closeOnce sync.Once
|
|
|
|
closeChan chan struct{}
|
|
|
|
}
|
|
|
|
|
2024-05-23 11:29:41 +08:00
|
|
|
func NewScheduler(manager TaskManager) Scheduler {
|
2024-04-01 20:09:13 +08:00
|
|
|
return &scheduler{
|
2024-01-31 20:45:04 +08:00
|
|
|
manager: manager,
|
|
|
|
closeChan: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:09:13 +08:00
|
|
|
func (s *scheduler) Start() {
|
|
|
|
log.Info("start import scheduler")
|
2024-01-31 20:45:04 +08:00
|
|
|
var (
|
|
|
|
exeTicker = time.NewTicker(1 * time.Second)
|
|
|
|
logTicker = time.NewTicker(10 * time.Minute)
|
|
|
|
)
|
|
|
|
defer exeTicker.Stop()
|
|
|
|
defer logTicker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
2024-04-01 20:09:13 +08:00
|
|
|
case <-s.closeChan:
|
|
|
|
log.Info("import scheduler exited")
|
2024-01-31 20:45:04 +08:00
|
|
|
return
|
|
|
|
case <-exeTicker.C:
|
2024-04-01 20:09:13 +08:00
|
|
|
tasks := s.manager.GetBy(WithStates(datapb.ImportTaskStateV2_Pending))
|
|
|
|
futures := make(map[int64][]*conc.Future[any])
|
2024-01-31 20:45:04 +08:00
|
|
|
for _, task := range tasks {
|
2024-05-23 11:29:41 +08:00
|
|
|
fs := task.Execute()
|
|
|
|
futures[task.GetTaskID()] = fs
|
|
|
|
tryFreeFutures(futures)
|
2024-04-01 20:09:13 +08:00
|
|
|
}
|
|
|
|
for taskID, fs := range futures {
|
|
|
|
err := conc.AwaitAll(fs...)
|
|
|
|
if err != nil {
|
2024-04-13 22:03:23 +08:00
|
|
|
continue
|
2024-04-01 20:09:13 +08:00
|
|
|
}
|
|
|
|
s.manager.Update(taskID, UpdateState(datapb.ImportTaskStateV2_Completed))
|
|
|
|
log.Info("preimport/import done", zap.Int64("taskID", taskID))
|
2024-01-31 20:45:04 +08:00
|
|
|
}
|
|
|
|
case <-logTicker.C:
|
2024-04-01 20:09:13 +08:00
|
|
|
LogStats(s.manager)
|
2024-01-31 20:45:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:09:13 +08:00
|
|
|
func (s *scheduler) Slots() int64 {
|
|
|
|
tasks := s.manager.GetBy(WithStates(datapb.ImportTaskStateV2_Pending, datapb.ImportTaskStateV2_InProgress))
|
2024-01-31 20:45:04 +08:00
|
|
|
return paramtable.Get().DataNodeCfg.MaxConcurrentImportTaskNum.GetAsInt64() - int64(len(tasks))
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:09:13 +08:00
|
|
|
func (s *scheduler) Close() {
|
|
|
|
s.closeOnce.Do(func() {
|
|
|
|
close(s.closeChan)
|
2024-01-31 20:45:04 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:09:13 +08:00
|
|
|
func tryFreeFutures(futures map[int64][]*conc.Future[any]) {
|
|
|
|
for k, fs := range futures {
|
|
|
|
fs = lo.Filter(fs, func(f *conc.Future[any], _ int) bool {
|
|
|
|
if f.Done() {
|
|
|
|
_, err := f.Await()
|
|
|
|
return err != nil
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
futures[k] = fs
|
|
|
|
}
|
|
|
|
}
|