handler.go 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

17 18 19 20 21
package datacoord

import (
	"context"

S
SimFG 已提交
22
	"github.com/milvus-io/milvus/api/commonpb"
23 24 25
	"github.com/milvus-io/milvus/internal/log"
	"github.com/milvus-io/milvus/internal/proto/datapb"
	"github.com/milvus-io/milvus/internal/proto/internalpb"
26
	"github.com/milvus-io/milvus/internal/util/funcutil"
27
	"github.com/milvus-io/milvus/internal/util/typeutil"
G
godchen 已提交
28
	"go.uber.org/zap"
29 30
)

31
// Handler handles some channel method for ChannelManager
32
type Handler interface {
33
	// GetVChanPositions gets the information recovery needed of a channel
34
	GetVChanPositions(channel *channel, partitionID UniqueID) *datapb.VchannelInfo
35
	CheckShouldDropChannel(channel string) bool
36
	FinishDropChannel(channel string)
37 38
}

39
// ServerHandler is a helper of Server
40 41 42 43
type ServerHandler struct {
	s *Server
}

44
// newServerHandler creates a new ServerHandler
45 46 47 48
func newServerHandler(s *Server) *ServerHandler {
	return &ServerHandler{s: s}
}

49 50 51
// GetVChanPositions gets vchannel latest postitions with provided dml channel names,
// we expect QueryCoord gets the indexed segments to load, so the flushed segments below are actually the indexed segments,
// the unflushed segments are actually the segments without index, even they are flushed.
52
func (h *ServerHandler) GetVChanPositions(channel *channel, partitionID UniqueID) *datapb.VchannelInfo {
53 54
	// cannot use GetSegmentsByChannel since dropped segments are needed here
	segments := h.s.meta.SelectSegments(func(s *SegmentInfo) bool {
55
		return s.InsertChannel == channel.Name
56
	})
57 58 59 60 61 62
	segmentInfos := make(map[int64]*SegmentInfo)
	indexedSegments := FilterInIndexedSegments(h.s.meta, h.s.indexCoord, segments...)
	indexed := make(typeutil.UniqueSet)
	for _, segment := range indexedSegments {
		indexed.Insert(segment.GetID())
	}
X
Xiaofan 已提交
63
	log.Info("GetSegmentsByChannel",
64
		zap.Any("collectionID", channel.CollectionID),
65 66 67
		zap.Any("channel", channel),
		zap.Any("numOfSegments", len(segments)),
	)
68
	var (
69 70 71
		indexedIDs   = make(typeutil.UniqueSet)
		unIndexedIDs = make(typeutil.UniqueSet)
		droppedIDs   = make(typeutil.UniqueSet)
72 73
		seekPosition *internalpb.MsgPosition
	)
74 75 76 77 78
	for _, s := range segments {
		if (partitionID > allPartitionID && s.PartitionID != partitionID) ||
			(s.GetStartPosition() == nil && s.GetDmlPosition() == nil) {
			continue
		}
79
		segmentInfos[s.GetID()] = s
80
		if s.GetState() == commonpb.SegmentState_Dropped {
81
			droppedIDs.Insert(s.GetID())
82
		} else if indexed.Contain(s.GetID()) {
83
			indexedIDs.Insert(s.GetID())
84
		} else {
85
			unIndexedIDs.Insert(s.GetID())
86 87
		}
	}
88
	for id := range unIndexedIDs {
89 90
		// Indexed segments are compacted to a raw segment,
		// replace it with the indexed ones
91
		if len(segmentInfos[id].GetCompactionFrom()) > 0 &&
92
			indexed.Contain(segmentInfos[id].GetCompactionFrom()...) {
93 94 95
			unIndexedIDs.Remove(id)
			indexedIDs.Insert(segmentInfos[id].GetCompactionFrom()...)
			droppedIDs.Remove(segmentInfos[id].GetCompactionFrom()...)
96
		}
97
	}
98

99
	for id := range indexedIDs {
100 101 102 103
		var segmentPosition *internalpb.MsgPosition
		segment := segmentInfos[id]
		if segment.GetDmlPosition() != nil {
			segmentPosition = segment.GetDmlPosition()
104
		} else {
105
			segmentPosition = segment.GetStartPosition()
106 107
		}

108 109 110 111
		if seekPosition == nil || segmentPosition.Timestamp < seekPosition.Timestamp {
			seekPosition = segmentPosition
		}
	}
112
	for id := range unIndexedIDs {
113
		var segmentPosition *internalpb.MsgPosition
114 115 116
		segment := segmentInfos[id]
		if segment.GetDmlPosition() != nil {
			segmentPosition = segment.GetDmlPosition()
117
		} else {
118
			segmentPosition = segment.GetStartPosition()
119 120 121 122 123 124
		}

		if seekPosition == nil || segmentPosition.Timestamp < seekPosition.Timestamp {
			seekPosition = segmentPosition
		}
	}
125

126 127
	// use collection start position when segment position is not found
	if seekPosition == nil {
128 129 130 131 132 133 134 135
		if channel.StartPositions == nil {
			collection := h.GetCollection(h.s.ctx, channel.CollectionID)
			if collection != nil {
				seekPosition = getCollectionStartPosition(channel.Name, collection)
			}
		} else {
			// use passed start positions, skip to ask rootcoord.
			seekPosition = toMsgPosition(channel.Name, channel.StartPositions)
136 137 138 139
		}
	}

	return &datapb.VchannelInfo{
140 141
		CollectionID:        channel.CollectionID,
		ChannelName:         channel.Name,
142
		SeekPosition:        seekPosition,
143 144 145
		FlushedSegmentIds:   indexedIDs.Collect(),
		UnflushedSegmentIds: unIndexedIDs.Collect(),
		DroppedSegmentIds:   droppedIDs.Collect(),
146 147 148 149
	}
}

func getCollectionStartPosition(channel string, collectionInfo *datapb.CollectionInfo) *internalpb.MsgPosition {
150 151 152 153 154
	return toMsgPosition(channel, collectionInfo.GetStartPositions())
}

func toMsgPosition(channel string, startPositions []*commonpb.KeyDataPair) *internalpb.MsgPosition {
	for _, sp := range startPositions {
155
		if sp.GetKey() != funcutil.ToPhysicalChannel(channel) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
			continue
		}
		return &internalpb.MsgPosition{
			ChannelName: channel,
			MsgID:       sp.GetData(),
		}
	}
	return nil
}

// trimSegmentInfo returns a shallow copy of datapb.SegmentInfo and sets ALL binlog info to nil
func trimSegmentInfo(info *datapb.SegmentInfo) *datapb.SegmentInfo {
	return &datapb.SegmentInfo{
		ID:             info.ID,
		CollectionID:   info.CollectionID,
		PartitionID:    info.PartitionID,
		InsertChannel:  info.InsertChannel,
		NumOfRows:      info.NumOfRows,
		State:          info.State,
		MaxRowNum:      info.MaxRowNum,
		LastExpireTime: info.LastExpireTime,
		StartPosition:  info.StartPosition,
		DmlPosition:    info.DmlPosition,
	}
}

182
// GetCollection returns collection info with specified collection id
183 184 185 186 187 188 189 190 191 192 193 194 195
func (h *ServerHandler) GetCollection(ctx context.Context, collectionID UniqueID) *datapb.CollectionInfo {
	coll := h.s.meta.GetCollection(collectionID)
	if coll != nil {
		return coll
	}
	err := h.s.loadCollectionFromRootCoord(ctx, collectionID)
	if err != nil {
		log.Warn("failed to load collection from rootcoord", zap.Int64("collectionID", collectionID), zap.Error(err))
	}

	return h.s.meta.GetCollection(collectionID)
}

196
// CheckShouldDropChannel returns whether specified channel is marked to be removed
197
func (h *ServerHandler) CheckShouldDropChannel(channel string) bool {
198 199 200 201 202 203 204 205 206 207 208 209 210
	/*
		segments := h.s.meta.GetSegmentsByChannel(channel)
		for _, segment := range segments {
			if segment.GetStartPosition() != nil && // filter empty segment
				// FIXME: we filter compaction generated segments
				// because datanode may not know the segment due to the network lag or
				// datacoord crash when handling CompleteCompaction.
				// FIXME: cancel this limitation for #12265
				// need to change a unified DropAndFlush to solve the root problem
				//len(segment.CompactionFrom) == 0 &&
				segment.GetState() != commonpb.SegmentState_Dropped {
				return false
			}
211
		}
212
		return false*/
213
	return h.s.meta.catalog.IsChannelDropped(h.s.ctx, channel)
214 215
}

216 217
// FinishDropChannel cleans up the remove flag for channels
// this function is a wrapper of server.meta.FinishDropChannel
218
func (h *ServerHandler) FinishDropChannel(channel string) {
219
	h.s.meta.catalog.DropChannel(h.s.ctx, channel)
220
}