2021-11-08 19:49:07 +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 datanode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-01-19 11:34:54 +08:00
|
|
|
"github.com/samber/lo"
|
2021-11-08 19:49:07 +08:00
|
|
|
"go.uber.org/zap"
|
2022-02-28 19:11:55 +08:00
|
|
|
|
2023-11-14 15:56:19 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
2022-10-20 16:39:29 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2023-07-24 10:23:01 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2021-11-08 19:49:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxTaskNum = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
type compactionExecutor struct {
|
2023-11-14 15:56:19 +08:00
|
|
|
executing *typeutil.ConcurrentMap[int64, compactor] // planID to compactor
|
|
|
|
completedCompactor *typeutil.ConcurrentMap[int64, compactor] // planID to compactor
|
|
|
|
completed *typeutil.ConcurrentMap[int64, *datapb.CompactionPlanResult] // planID to CompactionPlanResult
|
2023-03-17 17:27:56 +08:00
|
|
|
taskCh chan compactor
|
2023-07-24 10:23:01 +08:00
|
|
|
dropped *typeutil.ConcurrentSet[string] // vchannel dropped
|
2021-11-08 19:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCompactionExecutor() *compactionExecutor {
|
|
|
|
return &compactionExecutor{
|
2023-07-24 10:23:01 +08:00
|
|
|
executing: typeutil.NewConcurrentMap[int64, compactor](),
|
|
|
|
completedCompactor: typeutil.NewConcurrentMap[int64, compactor](),
|
2023-11-14 15:56:19 +08:00
|
|
|
completed: typeutil.NewConcurrentMap[int64, *datapb.CompactionPlanResult](),
|
2023-07-24 10:23:01 +08:00
|
|
|
taskCh: make(chan compactor, maxTaskNum),
|
|
|
|
dropped: typeutil.NewConcurrentSet[string](),
|
2021-11-08 19:49:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compactionExecutor) execute(task compactor) {
|
|
|
|
c.taskCh <- task
|
2023-02-15 16:00:33 +08:00
|
|
|
c.toExecutingState(task)
|
2021-11-08 19:49:07 +08:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:49:39 +08:00
|
|
|
func (c *compactionExecutor) toExecutingState(task compactor) {
|
2023-07-24 10:23:01 +08:00
|
|
|
c.executing.Insert(task.getPlanID(), task)
|
2022-01-18 17:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compactionExecutor) toCompleteState(task compactor) {
|
|
|
|
task.complete()
|
2023-07-24 10:23:01 +08:00
|
|
|
c.executing.GetAndRemove(task.getPlanID())
|
2022-01-18 17:49:39 +08:00
|
|
|
}
|
2023-06-20 10:20:41 +08:00
|
|
|
|
2023-11-29 10:50:29 +08:00
|
|
|
func (c *compactionExecutor) injectDone(planID UniqueID) {
|
2023-07-24 10:23:01 +08:00
|
|
|
c.completed.GetAndRemove(planID)
|
|
|
|
task, loaded := c.completedCompactor.GetAndRemove(planID)
|
2023-03-17 17:27:56 +08:00
|
|
|
if loaded {
|
2024-03-01 11:31:00 +08:00
|
|
|
log.Info("Compaction task inject done", zap.Int64("planID", planID), zap.String("channel", task.getChannelName()))
|
2023-11-29 10:50:29 +08:00
|
|
|
task.injectDone()
|
2023-03-17 17:27:56 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-18 17:49:39 +08:00
|
|
|
|
|
|
|
// These two func are bounded for waitGroup
|
|
|
|
func (c *compactionExecutor) executeWithState(task compactor) {
|
|
|
|
go c.executeTask(task)
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:49:07 +08:00
|
|
|
func (c *compactionExecutor) start(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case task := <-c.taskCh:
|
2022-01-18 17:49:39 +08:00
|
|
|
c.executeWithState(task)
|
2021-11-08 19:49:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compactionExecutor) executeTask(task compactor) {
|
2024-02-01 14:25:04 +08:00
|
|
|
log := log.With(
|
|
|
|
zap.Int64("planID", task.getPlanID()),
|
|
|
|
zap.Int64("Collection", task.getCollection()),
|
|
|
|
zap.String("channel", task.getChannelName()),
|
|
|
|
)
|
|
|
|
|
2021-11-08 19:49:07 +08:00
|
|
|
defer func() {
|
2022-01-18 17:49:39 +08:00
|
|
|
c.toCompleteState(task)
|
2021-11-08 19:49:07 +08:00
|
|
|
}()
|
|
|
|
|
2024-02-01 14:25:04 +08:00
|
|
|
log.Info("start to execute compaction")
|
2021-11-08 19:49:07 +08:00
|
|
|
|
2022-08-23 15:50:52 +08:00
|
|
|
result, err := task.compact()
|
2021-11-08 19:49:07 +08:00
|
|
|
if err != nil {
|
2024-02-01 14:25:04 +08:00
|
|
|
task.injectDone()
|
|
|
|
log.Warn("compaction task failed", zap.Error(err))
|
2022-08-23 15:50:52 +08:00
|
|
|
} else {
|
2023-11-14 15:56:19 +08:00
|
|
|
c.completed.Insert(result.GetPlanID(), result)
|
|
|
|
c.completedCompactor.Insert(result.GetPlanID(), task)
|
2021-11-08 19:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("end to execute compaction", zap.Int64("planID", task.getPlanID()))
|
|
|
|
}
|
2021-11-11 20:56:49 +08:00
|
|
|
|
|
|
|
func (c *compactionExecutor) stopTask(planID UniqueID) {
|
2023-07-24 10:23:01 +08:00
|
|
|
task, loaded := c.executing.GetAndRemove(planID)
|
2021-11-11 20:56:49 +08:00
|
|
|
if loaded {
|
2023-07-24 10:23:01 +08:00
|
|
|
log.Warn("compaction executor stop task", zap.Int64("planID", planID), zap.String("vChannelName", task.getChannelName()))
|
|
|
|
task.stop()
|
2021-11-11 20:56:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 10:50:29 +08:00
|
|
|
func (c *compactionExecutor) isValidChannel(channel string) bool {
|
2021-12-02 16:39:33 +08:00
|
|
|
// if vchannel marked dropped, compaction should not proceed
|
2023-11-29 10:50:29 +08:00
|
|
|
return !c.dropped.Contain(channel)
|
2021-12-02 16:39:33 +08:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:50:29 +08:00
|
|
|
func (c *compactionExecutor) clearTasksByChannel(channel string) {
|
|
|
|
c.dropped.Insert(channel)
|
|
|
|
|
|
|
|
// stop executing tasks of channel
|
2023-07-24 10:23:01 +08:00
|
|
|
c.executing.Range(func(planID int64, task compactor) bool {
|
2023-11-29 10:50:29 +08:00
|
|
|
if task.getChannelName() == channel {
|
2023-07-24 10:23:01 +08:00
|
|
|
c.stopTask(planID)
|
2021-11-11 20:56:49 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2023-11-14 15:56:19 +08:00
|
|
|
|
2023-11-29 10:50:29 +08:00
|
|
|
// remove all completed plans of channel
|
2023-11-14 15:56:19 +08:00
|
|
|
c.completed.Range(func(planID int64, result *datapb.CompactionPlanResult) bool {
|
2023-11-29 10:50:29 +08:00
|
|
|
if result.GetChannel() == channel {
|
|
|
|
c.injectDone(planID)
|
2022-10-20 16:39:29 +08:00
|
|
|
log.Info("remove compaction results for dropped channel",
|
2023-11-29 10:50:29 +08:00
|
|
|
zap.String("channel", channel),
|
2023-07-24 10:23:01 +08:00
|
|
|
zap.Int64("planID", planID))
|
2022-10-20 16:39:29 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2021-11-11 20:56:49 +08:00
|
|
|
}
|
2023-11-14 15:56:19 +08:00
|
|
|
|
2023-11-29 10:50:29 +08:00
|
|
|
func (c *compactionExecutor) getAllCompactionResults() []*datapb.CompactionPlanResult {
|
2024-01-19 11:34:54 +08:00
|
|
|
var (
|
|
|
|
executing []int64
|
|
|
|
completed []int64
|
|
|
|
completedLevelZero []int64
|
|
|
|
)
|
2023-11-14 15:56:19 +08:00
|
|
|
results := make([]*datapb.CompactionPlanResult, 0)
|
2023-11-29 10:50:29 +08:00
|
|
|
// get executing results
|
2023-11-14 15:56:19 +08:00
|
|
|
c.executing.Range(func(planID int64, task compactor) bool {
|
2024-01-19 11:34:54 +08:00
|
|
|
executing = append(executing, planID)
|
2023-11-14 15:56:19 +08:00
|
|
|
results = append(results, &datapb.CompactionPlanResult{
|
|
|
|
State: commonpb.CompactionState_Executing,
|
|
|
|
PlanID: planID,
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2023-11-29 10:50:29 +08:00
|
|
|
// get completed results
|
2023-11-14 15:56:19 +08:00
|
|
|
c.completed.Range(func(planID int64, result *datapb.CompactionPlanResult) bool {
|
2024-01-19 11:34:54 +08:00
|
|
|
completed = append(completed, planID)
|
2023-11-14 15:56:19 +08:00
|
|
|
results = append(results, result)
|
2024-01-19 11:34:54 +08:00
|
|
|
|
|
|
|
if result.GetType() == datapb.CompactionType_Level0DeleteCompaction {
|
|
|
|
completedLevelZero = append(completedLevelZero, planID)
|
|
|
|
}
|
2023-11-14 15:56:19 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2024-01-19 11:34:54 +08:00
|
|
|
// remote level zero results
|
|
|
|
lo.ForEach(completedLevelZero, func(planID int64, _ int) {
|
|
|
|
c.completed.Remove(planID)
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(results) > 0 {
|
|
|
|
log.Info("DataNode Compaction results",
|
|
|
|
zap.Int64s("executing", executing),
|
|
|
|
zap.Int64s("completed", completed),
|
|
|
|
zap.Int64s("completed levelzero", completedLevelZero),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-11-14 15:56:19 +08:00
|
|
|
return results
|
|
|
|
}
|