handler.go 11.2 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-proto/go-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/tsoutil"
28
	"github.com/milvus-io/milvus/internal/util/typeutil"
G
godchen 已提交
29
	"go.uber.org/zap"
30 31
)

32
// Handler handles some channel method for ChannelManager
33
type Handler interface {
X
XuanYang-cn 已提交
34 35 36 37
	// GetQueryVChanPositions gets the information recovery needed of a channel for QueryCoord
	GetQueryVChanPositions(channel *channel, partitionID UniqueID) *datapb.VchannelInfo
	// GetDataVChanPositions gets the information recovery needed of a channel for DataNode
	GetDataVChanPositions(channel *channel, partitionID UniqueID) *datapb.VchannelInfo
38
	CheckShouldDropChannel(channel string) bool
39
	FinishDropChannel(channel string)
40
	GetCollection(ctx context.Context, collectionID UniqueID) (*collectionInfo, error)
41 42
}

43
// ServerHandler is a helper of Server
44 45 46 47
type ServerHandler struct {
	s *Server
}

48
// newServerHandler creates a new ServerHandler
49 50 51 52
func newServerHandler(s *Server) *ServerHandler {
	return &ServerHandler{s: s}
}

X
XuanYang-cn 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// GetDataVChanPositions gets vchannel latest postitions with provided dml channel names for DataNode.
func (h *ServerHandler) GetDataVChanPositions(channel *channel, partitionID UniqueID) *datapb.VchannelInfo {
	segments := h.s.meta.SelectSegments(func(s *SegmentInfo) bool {
		return s.InsertChannel == channel.Name
	})
	log.Info("GetDataVChanPositions",
		zap.Int64("collectionID", channel.CollectionID),
		zap.String("channel", channel.Name),
		zap.Int("numOfSegments", len(segments)),
	)
	var (
		flushedIDs   = make(typeutil.UniqueSet)
		unflushedIDs = make(typeutil.UniqueSet)
		droppedIDs   = make(typeutil.UniqueSet)
		seekPosition *internalpb.MsgPosition
	)
69 70
	var minPosSegID int64
	var minPosTs uint64
X
XuanYang-cn 已提交
71 72 73 74 75 76
	for _, s := range segments {
		if (partitionID > allPartitionID && s.PartitionID != partitionID) ||
			(s.GetStartPosition() == nil && s.GetDmlPosition() == nil) {
			continue
		}
		if s.GetIsImporting() {
G
groot 已提交
77
			// Skip bulk insert segments.
X
XuanYang-cn 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			continue
		}

		if s.GetState() == commonpb.SegmentState_Dropped {
			droppedIDs.Insert(s.GetID())
			continue
		} else if s.GetState() == commonpb.SegmentState_Flushing || s.GetState() == commonpb.SegmentState_Flushed {
			flushedIDs.Insert(s.GetID())
		} else {
			unflushedIDs.Insert(s.GetID())
		}

		var segmentPosition *internalpb.MsgPosition
		if s.GetDmlPosition() != nil {
			segmentPosition = s.GetDmlPosition()
		} else {
			segmentPosition = s.GetStartPosition()
		}
		if seekPosition == nil || segmentPosition.Timestamp < seekPosition.Timestamp {
97 98
			minPosSegID = s.GetID()
			minPosTs = segmentPosition.GetTimestamp()
X
XuanYang-cn 已提交
99 100 101 102
			seekPosition = segmentPosition
		}
	}

103 104 105 106 107 108 109 110
	if seekPosition != nil {
		log.Info("channel seek position set as the minimal segment position",
			zap.Int64("segment ID", minPosSegID),
			zap.Uint64("position timestamp", minPosTs),
			zap.String("realworld position timestamp", tsoutil.ParseAndFormatHybridTs(minPosTs)),
		)
	} else {
		// use collection start position when segment position is not found
X
XuanYang-cn 已提交
111
		if channel.StartPositions == nil {
112 113
			collection, err := h.GetCollection(h.s.ctx, channel.CollectionID)
			if collection != nil && err == nil {
X
XuanYang-cn 已提交
114 115
				seekPosition = getCollectionStartPosition(channel.Name, collection)
			}
116 117 118 119
			log.Info("NEITHER segment position or channel start position are found, setting channel seek position to collection start position",
				zap.Uint64("position timestamp", seekPosition.GetTimestamp()),
				zap.String("realworld position timestamp", tsoutil.ParseAndFormatHybridTs(seekPosition.GetTimestamp())),
			)
X
XuanYang-cn 已提交
120
		} else {
121
			// use passed start positions, skip to ask RootCoord.
X
XuanYang-cn 已提交
122
			seekPosition = toMsgPosition(channel.Name, channel.StartPositions)
123 124 125 126
			log.Info("segment position not found, setting channel seek position to channel start position",
				zap.Uint64("position timestamp", seekPosition.GetTimestamp()),
				zap.String("realworld position timestamp", tsoutil.ParseAndFormatHybridTs(seekPosition.GetTimestamp())),
			)
X
XuanYang-cn 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
		}
	}

	return &datapb.VchannelInfo{
		CollectionID:        channel.CollectionID,
		ChannelName:         channel.Name,
		SeekPosition:        seekPosition,
		FlushedSegmentIds:   flushedIDs.Collect(),
		UnflushedSegmentIds: unflushedIDs.Collect(),
		DroppedSegmentIds:   droppedIDs.Collect(),
	}
}

// GetQueryVChanPositions gets vchannel latest postitions with provided dml channel names for QueryCoord,
141 142
// 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.
X
XuanYang-cn 已提交
143
func (h *ServerHandler) GetQueryVChanPositions(channel *channel, partitionID UniqueID) *datapb.VchannelInfo {
144 145
	// cannot use GetSegmentsByChannel since dropped segments are needed here
	segments := h.s.meta.SelectSegments(func(s *SegmentInfo) bool {
146
		return s.InsertChannel == channel.Name
147
	})
148
	segmentInfos := make(map[int64]*SegmentInfo)
149
	indexedSegments := FilterInIndexedSegments(h, h.s.indexCoord, segments...)
150 151 152 153
	indexed := make(typeutil.UniqueSet)
	for _, segment := range indexedSegments {
		indexed.Insert(segment.GetID())
	}
X
XuanYang-cn 已提交
154 155 156 157
	log.Info("GetQueryVChanPositions",
		zap.Int64("collectionID", channel.CollectionID),
		zap.String("channel", channel.Name),
		zap.Int("numOfSegments", len(segments)),
158
	)
159
	var (
160 161 162
		indexedIDs   = make(typeutil.UniqueSet)
		unIndexedIDs = make(typeutil.UniqueSet)
		droppedIDs   = make(typeutil.UniqueSet)
163 164
		seekPosition *internalpb.MsgPosition
	)
165 166 167 168 169
	for _, s := range segments {
		if (partitionID > allPartitionID && s.PartitionID != partitionID) ||
			(s.GetStartPosition() == nil && s.GetDmlPosition() == nil) {
			continue
		}
170
		if s.GetIsImporting() {
G
groot 已提交
171
			// Skip bulk insert segments.
172 173
			continue
		}
174
		segmentInfos[s.GetID()] = s
175
		if s.GetState() == commonpb.SegmentState_Dropped {
176
			droppedIDs.Insert(s.GetID())
177
		} else if indexed.Contain(s.GetID()) {
178
			indexedIDs.Insert(s.GetID())
179
		} else {
180
			unIndexedIDs.Insert(s.GetID())
181 182
		}
	}
183
	for id := range unIndexedIDs {
184 185
		// Indexed segments are compacted to a raw segment,
		// replace it with the indexed ones
186
		if len(segmentInfos[id].GetCompactionFrom()) > 0 &&
187
			indexed.Contain(segmentInfos[id].GetCompactionFrom()...) {
188 189 190
			unIndexedIDs.Remove(id)
			indexedIDs.Insert(segmentInfos[id].GetCompactionFrom()...)
			droppedIDs.Remove(segmentInfos[id].GetCompactionFrom()...)
191
		}
192
	}
193

194
	for id := range indexedIDs {
195 196 197 198
		var segmentPosition *internalpb.MsgPosition
		segment := segmentInfos[id]
		if segment.GetDmlPosition() != nil {
			segmentPosition = segment.GetDmlPosition()
199
		} else {
200
			segmentPosition = segment.GetStartPosition()
201 202
		}

203 204 205 206
		if seekPosition == nil || segmentPosition.Timestamp < seekPosition.Timestamp {
			seekPosition = segmentPosition
		}
	}
207
	for id := range unIndexedIDs {
208
		var segmentPosition *internalpb.MsgPosition
209 210 211
		segment := segmentInfos[id]
		if segment.GetDmlPosition() != nil {
			segmentPosition = segment.GetDmlPosition()
212
		} else {
213
			segmentPosition = segment.GetStartPosition()
214 215 216 217 218 219
		}

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

221 222
	// use collection start position when segment position is not found
	if seekPosition == nil {
223
		if channel.StartPositions == nil {
224 225
			collection, err := h.GetCollection(h.s.ctx, channel.CollectionID)
			if collection != nil && err == nil {
226 227 228 229 230
				seekPosition = getCollectionStartPosition(channel.Name, collection)
			}
		} else {
			// use passed start positions, skip to ask rootcoord.
			seekPosition = toMsgPosition(channel.Name, channel.StartPositions)
231 232 233 234
		}
	}

	return &datapb.VchannelInfo{
235 236
		CollectionID:        channel.CollectionID,
		ChannelName:         channel.Name,
237
		SeekPosition:        seekPosition,
238 239 240
		FlushedSegmentIds:   indexedIDs.Collect(),
		UnflushedSegmentIds: unIndexedIDs.Collect(),
		DroppedSegmentIds:   droppedIDs.Collect(),
241 242 243
	}
}

J
jaime 已提交
244 245
func getCollectionStartPosition(channel string, collectionInfo *collectionInfo) *internalpb.MsgPosition {
	return toMsgPosition(channel, collectionInfo.StartPositions)
246 247 248 249
}

func toMsgPosition(channel string, startPositions []*commonpb.KeyDataPair) *internalpb.MsgPosition {
	for _, sp := range startPositions {
250
		if sp.GetKey() != funcutil.ToPhysicalChannel(channel) {
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
			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,
	}
}

277
// GetCollection returns collection info with specified collection id
278
func (h *ServerHandler) GetCollection(ctx context.Context, collectionID UniqueID) (*collectionInfo, error) {
279 280
	coll := h.s.meta.GetCollection(collectionID)
	if coll != nil {
281
		return coll, nil
282 283 284 285
	}
	err := h.s.loadCollectionFromRootCoord(ctx, collectionID)
	if err != nil {
		log.Warn("failed to load collection from rootcoord", zap.Int64("collectionID", collectionID), zap.Error(err))
286
		return nil, err
287 288
	}

289
	return h.s.meta.GetCollection(collectionID), nil
290 291
}

292
// CheckShouldDropChannel returns whether specified channel is marked to be removed
293
func (h *ServerHandler) CheckShouldDropChannel(channel string) bool {
294 295 296 297 298 299 300 301 302 303 304 305 306
	/*
		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
			}
307
		}
308
		return false*/
309
	return h.s.meta.catalog.IsChannelDropped(h.s.ctx, channel)
310 311
}

312 313
// FinishDropChannel cleans up the remove flag for channels
// this function is a wrapper of server.meta.FinishDropChannel
314
func (h *ServerHandler) FinishDropChannel(channel string) {
315
	h.s.meta.catalog.DropChannel(h.s.ctx, channel)
316
}