2023-02-22 11:37:45 +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.
|
|
|
|
|
2022-09-05 13:29:11 +08:00
|
|
|
package rootcoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/msgpb"
|
2023-03-04 23:21:50 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/model"
|
2023-04-06 19:14:32 +08:00
|
|
|
ms "github.com/milvus-io/milvus/pkg/mq/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/commonpbutil"
|
2022-09-05 13:29:11 +08:00
|
|
|
)
|
|
|
|
|
2023-05-08 10:18:39 +08:00
|
|
|
//go:generate mockery --name=GarbageCollector --outpkg=mockrootcoord --filename=garbage_collector.go --with-expecter --testonly
|
2022-09-05 13:29:11 +08:00
|
|
|
type GarbageCollector interface {
|
|
|
|
ReDropCollection(collMeta *model.Collection, ts Timestamp)
|
|
|
|
RemoveCreatingCollection(collMeta *model.Collection)
|
2023-06-25 17:20:43 +08:00
|
|
|
ReDropPartition(dbID int64, pChannels []string, partition *model.Partition, ts Timestamp)
|
|
|
|
RemoveCreatingPartition(dbID int64, partition *model.Partition, ts Timestamp)
|
2022-09-21 15:46:51 +08:00
|
|
|
GcCollectionData(ctx context.Context, coll *model.Collection) (ddlTs Timestamp, err error)
|
|
|
|
GcPartitionData(ctx context.Context, pChannels []string, partition *model.Partition) (ddlTs Timestamp, err error)
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
type bgGarbageCollector struct {
|
2022-09-05 13:29:11 +08:00
|
|
|
s *Core
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
func newBgGarbageCollector(s *Core) *bgGarbageCollector {
|
|
|
|
return &bgGarbageCollector{s: s}
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
func (c *bgGarbageCollector) ReDropCollection(collMeta *model.Collection, ts Timestamp) {
|
2022-09-05 13:29:11 +08:00
|
|
|
// TODO: remove this after data gc can be notified by rpc.
|
|
|
|
c.s.chanTimeTick.addDmlChannels(collMeta.PhysicalChannelNames...)
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
redo := newBaseRedoTask(c.s.stepExecutor)
|
|
|
|
redo.AddAsyncStep(&releaseCollectionStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
collectionID: collMeta.CollectionID,
|
|
|
|
})
|
|
|
|
redo.AddAsyncStep(&dropIndexStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
collID: collMeta.CollectionID,
|
2022-09-23 09:36:51 +08:00
|
|
|
partIDs: nil,
|
2022-09-21 15:46:51 +08:00
|
|
|
})
|
|
|
|
redo.AddAsyncStep(&deleteCollectionDataStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
coll: collMeta,
|
|
|
|
})
|
|
|
|
redo.AddAsyncStep(&removeDmlChannelsStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
pChannels: collMeta.PhysicalChannelNames,
|
|
|
|
})
|
2023-07-10 10:16:26 +08:00
|
|
|
redo.AddAsyncStep(newConfirmGCStep(c.s, collMeta.CollectionID, allPartition))
|
2022-09-21 15:46:51 +08:00
|
|
|
redo.AddAsyncStep(&deleteCollectionMetaStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
collectionID: collMeta.CollectionID,
|
2022-09-22 17:36:52 +08:00
|
|
|
// This ts is less than the ts when we notify data nodes to drop collection, but it's OK since we have already
|
|
|
|
// marked this collection as deleted. If we want to make this ts greater than the notification's ts, we should
|
|
|
|
// wrap a step who will have these three children and connect them with ts.
|
|
|
|
ts: ts,
|
2022-09-21 15:46:51 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// err is ignored since no sync steps will be executed.
|
|
|
|
_ = redo.Execute(context.Background())
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
func (c *bgGarbageCollector) RemoveCreatingCollection(collMeta *model.Collection) {
|
2022-09-23 16:56:50 +08:00
|
|
|
// TODO: remove this after data gc can be notified by rpc.
|
|
|
|
c.s.chanTimeTick.addDmlChannels(collMeta.PhysicalChannelNames...)
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
redo := newBaseRedoTask(c.s.stepExecutor)
|
2022-09-23 16:56:50 +08:00
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
redo.AddAsyncStep(&unwatchChannelsStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
collectionID: collMeta.CollectionID,
|
|
|
|
channels: collectionChannels{
|
|
|
|
virtualChannels: collMeta.VirtualChannelNames,
|
|
|
|
physicalChannels: collMeta.PhysicalChannelNames,
|
|
|
|
},
|
|
|
|
})
|
2022-09-23 16:56:50 +08:00
|
|
|
redo.AddAsyncStep(&removeDmlChannelsStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
pChannels: collMeta.PhysicalChannelNames,
|
|
|
|
})
|
2022-09-21 15:46:51 +08:00
|
|
|
redo.AddAsyncStep(&deleteCollectionMetaStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
collectionID: collMeta.CollectionID,
|
2022-09-23 16:56:50 +08:00
|
|
|
// When we undo createCollectionTask, this ts may be less than the ts when unwatch channels.
|
|
|
|
ts: collMeta.CreateTime,
|
2022-09-21 15:46:51 +08:00
|
|
|
})
|
|
|
|
// err is ignored since no sync steps will be executed.
|
|
|
|
_ = redo.Execute(context.Background())
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
func (c *bgGarbageCollector) ReDropPartition(dbID int64, pChannels []string, partition *model.Partition, ts Timestamp) {
|
2022-09-05 13:29:11 +08:00
|
|
|
// TODO: remove this after data gc can be notified by rpc.
|
|
|
|
c.s.chanTimeTick.addDmlChannels(pChannels...)
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
redo := newBaseRedoTask(c.s.stepExecutor)
|
|
|
|
redo.AddAsyncStep(&deletePartitionDataStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
pchans: pChannels,
|
|
|
|
partition: partition,
|
|
|
|
})
|
|
|
|
redo.AddAsyncStep(&removeDmlChannelsStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
pChannels: pChannels,
|
|
|
|
})
|
2023-07-10 10:16:26 +08:00
|
|
|
redo.AddAsyncStep(newConfirmGCStep(c.s, partition.CollectionID, partition.PartitionID))
|
2022-09-21 15:46:51 +08:00
|
|
|
redo.AddAsyncStep(&removePartitionMetaStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
2023-06-25 17:20:43 +08:00
|
|
|
dbID: dbID,
|
2022-09-21 15:46:51 +08:00
|
|
|
collectionID: partition.CollectionID,
|
|
|
|
partitionID: partition.PartitionID,
|
2022-09-22 17:36:52 +08:00
|
|
|
// This ts is less than the ts when we notify data nodes to drop partition, but it's OK since we have already
|
|
|
|
// marked this partition as deleted. If we want to make this ts greater than the notification's ts, we should
|
|
|
|
// wrap a step who will have these children and connect them with ts.
|
|
|
|
ts: ts,
|
2022-09-21 15:46:51 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// err is ignored since no sync steps will be executed.
|
|
|
|
_ = redo.Execute(context.Background())
|
|
|
|
}
|
2022-09-05 13:29:11 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
func (c *bgGarbageCollector) RemoveCreatingPartition(dbID int64, partition *model.Partition, ts Timestamp) {
|
2023-05-08 10:18:39 +08:00
|
|
|
redoTask := newBaseRedoTask(c.s.stepExecutor)
|
|
|
|
|
|
|
|
redoTask.AddAsyncStep(&releasePartitionsStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
|
|
|
collectionID: partition.CollectionID,
|
|
|
|
partitionIDs: []int64{partition.PartitionID},
|
|
|
|
})
|
|
|
|
|
|
|
|
redoTask.AddAsyncStep(&removePartitionMetaStep{
|
|
|
|
baseStep: baseStep{core: c.s},
|
2023-06-25 17:20:43 +08:00
|
|
|
dbID: dbID,
|
2023-05-08 10:18:39 +08:00
|
|
|
collectionID: partition.CollectionID,
|
|
|
|
partitionID: partition.PartitionID,
|
|
|
|
ts: ts,
|
|
|
|
})
|
|
|
|
|
|
|
|
// err is ignored since no sync steps will be executed.
|
|
|
|
_ = redoTask.Execute(context.Background())
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
func (c *bgGarbageCollector) notifyCollectionGc(ctx context.Context, coll *model.Collection) (ddlTs Timestamp, err error) {
|
|
|
|
ts, err := c.s.tsoAllocator.GenerateTSO(1)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
msgPack := ms.MsgPack{}
|
|
|
|
msg := &ms.DropCollectionMsg{
|
2023-08-21 19:16:22 +08:00
|
|
|
BaseMsg: ms.BaseMsg{
|
|
|
|
Ctx: ctx,
|
|
|
|
BeginTimestamp: ts,
|
|
|
|
EndTimestamp: ts,
|
|
|
|
HashValues: []uint32{0},
|
|
|
|
},
|
2023-03-04 23:21:50 +08:00
|
|
|
DropCollectionRequest: msgpb.DropCollectionRequest{
|
2022-10-21 15:57:28 +08:00
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_DropCollection),
|
|
|
|
commonpbutil.WithTimeStamp(ts),
|
|
|
|
commonpbutil.WithSourceID(c.s.session.ServerID),
|
|
|
|
),
|
2022-09-05 13:29:11 +08:00
|
|
|
CollectionName: coll.Name,
|
|
|
|
CollectionID: coll.CollectionID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, msg)
|
|
|
|
if err := c.s.chanTimeTick.broadcastDmlChannels(coll.PhysicalChannelNames, &msgPack); err != nil {
|
2022-09-21 15:46:51 +08:00
|
|
|
return 0, err
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
return ts, nil
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
func (c *bgGarbageCollector) notifyPartitionGc(ctx context.Context, pChannels []string, partition *model.Partition) (ddlTs Timestamp, err error) {
|
|
|
|
ts, err := c.s.tsoAllocator.GenerateTSO(1)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:29:11 +08:00
|
|
|
msgPack := ms.MsgPack{}
|
|
|
|
msg := &ms.DropPartitionMsg{
|
2023-08-21 19:16:22 +08:00
|
|
|
BaseMsg: ms.BaseMsg{
|
|
|
|
Ctx: ctx,
|
|
|
|
BeginTimestamp: ts,
|
|
|
|
EndTimestamp: ts,
|
|
|
|
HashValues: []uint32{0},
|
|
|
|
},
|
2023-03-04 23:21:50 +08:00
|
|
|
DropPartitionRequest: msgpb.DropPartitionRequest{
|
2022-10-21 15:57:28 +08:00
|
|
|
Base: commonpbutil.NewMsgBase(
|
|
|
|
commonpbutil.WithMsgType(commonpb.MsgType_DropPartition),
|
|
|
|
commonpbutil.WithTimeStamp(ts),
|
|
|
|
commonpbutil.WithSourceID(c.s.session.ServerID),
|
|
|
|
),
|
2022-09-05 13:29:11 +08:00
|
|
|
PartitionName: partition.PartitionName,
|
|
|
|
CollectionID: partition.CollectionID,
|
|
|
|
PartitionID: partition.PartitionID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, msg)
|
|
|
|
if err := c.s.chanTimeTick.broadcastDmlChannels(pChannels, &msgPack); err != nil {
|
2022-09-21 15:46:51 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bgGarbageCollector) GcCollectionData(ctx context.Context, coll *model.Collection) (ddlTs Timestamp, err error) {
|
|
|
|
c.s.ddlTsLockManager.Lock()
|
|
|
|
c.s.ddlTsLockManager.AddRefCnt(1)
|
|
|
|
defer c.s.ddlTsLockManager.AddRefCnt(-1)
|
|
|
|
defer c.s.ddlTsLockManager.Unlock()
|
|
|
|
|
|
|
|
ddlTs, err = c.notifyCollectionGc(ctx, coll)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
2022-09-21 15:46:51 +08:00
|
|
|
c.s.ddlTsLockManager.UpdateLastTs(ddlTs)
|
|
|
|
return ddlTs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bgGarbageCollector) GcPartitionData(ctx context.Context, pChannels []string, partition *model.Partition) (ddlTs Timestamp, err error) {
|
|
|
|
c.s.ddlTsLockManager.Lock()
|
|
|
|
c.s.ddlTsLockManager.AddRefCnt(1)
|
|
|
|
defer c.s.ddlTsLockManager.AddRefCnt(-1)
|
|
|
|
defer c.s.ddlTsLockManager.Unlock()
|
2022-09-05 13:29:11 +08:00
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
ddlTs, err = c.notifyPartitionGc(ctx, pChannels, partition)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
c.s.ddlTsLockManager.UpdateLastTs(ddlTs)
|
|
|
|
return ddlTs, nil
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|